Dynamic Routes
If your site has pages that aren't Statamic content, such as your own Laravel routes or pages built from an external API, you can add them to the sitemap as dynamic routes.
Registering dynamic routes
Call Sitemapamic::addDynamicRoutes() from the boot method of a service provider, such as your AppServiceProvider. Pass a closure that returns an array (or any iterable, including a generator) of SitemapamicUrl objects.
1php 2use Illuminate\Support\Carbon; 3use MityDigital\Sitemapamic\Facades\Sitemapamic; 4use MityDigital\Sitemapamic\Models\SitemapamicUrl; 5 6public function boot(): void 7{ 8 Sitemapamic::addDynamicRoutes(function (): array { 9 return [10 new SitemapamicUrl(11 loc: route('events.index'),12 lastmod: Carbon::now()->toW3cString(),13 ),14 new SitemapamicUrl(15 loc: route('events.archive'),16 ),17 ];18 });19}
The closure is not run when you register it. It runs only when a sitemap is built, so you can use route(), models, queries and API calls inside it. You do not need a separate service provider or $this->app->booted().
Using named routes (with route()) is recommended: if a route's URI changes, your sitemap follows it.
You can call addDynamicRoutes() as many times as you like. Each closure's URLs are added to the same dynamic routes sitemap.
The SitemapamicUrl object
When adding dynamic routes, you add a number of SitemapamicUrl objects.
Check out the full documentation for a complete explanation of the object’s properties.
Building URLs from your own data
The closure can build as many URLs as you need. For large sets, return a generator so URLs are written one at a time instead of being held in memory:
1use App\Models\Event; 2use MityDigital\Sitemapamic\Facades\Sitemapamic; 3use MityDigital\Sitemapamic\Models\SitemapamicUrl; 4 5Sitemapamic::addDynamicRoutes(function (): Generator { 6 foreach (Event::query()->published()->lazy() as $event) { 7 yield new SitemapamicUrl( 8 loc: route('events.show', $event), 9 lastmod: $event->updated_at->toW3cString(),10 );11 }12});
Multi-site
By default, a closure is used for every site. Pass a site handle, or an array of handles, as the second argument to limit it to those sites. null or '*' means every site.
1// only the "default" site2Sitemapamic::addDynamicRoutes(fn (): array => [/* … */], 'default');3 4// the "au" and "nz" sites5Sitemapamic::addDynamicRoutes(fn (): array => [/* … */], ['au', 'nz']);
The closure receives the Site the sitemap is being built for. $site->absoluteUrl() always returns an absolute URL, even when the site's URL is configured as relative, so you can use it to build URLs for each site:
1use MityDigital\Sitemapamic\Facades\Sitemapamic; 2use MityDigital\Sitemapamic\Models\SitemapamicUrl; 3use Statamic\Sites\Site; 4 5Sitemapamic::addDynamicRoutes(function (Site $site): array { 6 $base = rtrim($site->absoluteUrl(), '/'); 7 8 return [ 9 new SitemapamicUrl(loc: $base.'/events'),10 ];11});
Where dynamic routes appear
In
automaticmode, dynamic routes get their ownsitemap-dynamic-1.xmlfile, listed in thesitemap.xmlindex. Very large sets are split intositemap-dynamic-2.xmland so on.In
singlemode, they are merged intositemap.xmlwith the rest of your content.
Run php please sitemapamic:list to see the files for each site.
Keeping dynamic routes fresh
Dynamic routes are cached with the rest of the sitemap. Sitemapamic rebuilds the sitemap when Statamic content changes, but it doesn’t automatically know when your own data changes.
To work around this, you can mark the sitemap stale when your data changes, for example in a model observer or after an import. The next request rebuilds the sitemap, and the previous version is still served to any requests that arrive while it rebuilds.
1use MityDigital\Sitemapamic\Facades\Sitemapamic;2 3Sitemapamic::markStale(); // every site4Sitemapamic::markStale('default'); // one site
You can also set a ttl for the cache to automatically expire, or run a scheduled task calling the clear command.