Mity Docs Mity Docs for Mity Digital
Sitemapamic Documentation

Dynamic Routes

If your site has routes that are not part of Statamic - such as your own custom Laravel routes - you can add these to you sitemap too.

Configuring Dynamic Routes

In your AppServiceProvider (or you can create your own SitemapamicServiceProvider if you like too - especially if you use named routes, keep reading) you can add dynamic routes to Sitemapamic as a closure that returns an array of SitemapamicUrl objects:

1Sitemapamic::addDynamicRoutes(function() {
2 return [
3 new SitemapamicUrl(
4 'https://my-awesome-url/dynamic-route',
5 Carbon::now()->toW3cString()
6 ),
7 new SitemapamicUrl(
8 'https://my-awesome-url/a-different-dynamic-route',
9 Carbon::now()->toW3cString()
10 )
11 ];
12});

SitemapamicUrl object

Each SitemapamicUrl expects:

  • loc, required, a string with a full URL to include

  • lastmod, required, a string in the date format you want (we use Carbon's toW3cString internally)

  • changefreq, optional, a value for your the <changefreq> element

  • priority, optional, a value between 0.0 and 1.0 for the <priority> element

Your loc can be whatever you need it to be for your app - including building dynamic URLs based on your app's own data. How many you add and how you build them is totally up to you.

Using named routes in a provider

It would be our recommendation to use Named Routes where you can - so that if you change your route, your sitemap can pick it automatically. It also means you don't have to be hardcoding full URLs in your app.

To do this, you need a little bit of extra work. Firstly, create your own Service Provider, and make sure it is in your app's config after the RouteServiceProvider.

Within your Service Provider's boot method, you can add Dynamic Routes to Sitemapamic after the app has booted:

1$this->app->booted(function () {
2 Sitemapamic::addDynamicRoutes(function () {
3 return [
4 new SitemapamicUrl(
5 route('dynamic-route'),
6 Carbon::now()->toW3cString()
7 )
8 ];
9 });
10});