Combining different routes

The following two route setups work perfectly when i use them solo. I would like to merge them in the same config file, but i can’t get it to work. What am i missing?

The code:

<?php

return [
  'routes' => [
    [
      'pattern' => '(:any)',
      'action'  => function($uid) {
          $page = page($uid);
          if(!$page) $page = page('blog/' . $uid);
          if(!$page) $page = page('contact/' . $uid);
          if(!$page) $page = site()->errorPage();
          return $page;
      }
    ],
    [
      'pattern' => 'blog/(:any)',
      'action'  => function($uid) {
          go($uid);
      }
    ],
    [
      'pattern' => 'contact/(:any)',
      'action'  => function($uid) {
          go($uid);
      }
    ],
  ],
];

and:


<?php

return [
  'routes' => [
    [
      'pattern' => 'sitemap.xml',
      'action'  => function() {
          $pages = site()->pages()->index();
          // fetch the pages to ignore from the config settings,
          // if nothing is set, we ignore the error page
          $ignore = kirby()->option('sitemap.ignore', ['error']);
          $content = snippet('sitemap', compact('pages', 'ignore'), true);
          // return response with correct header type
          return new Kirby\Cms\Response($content, 'application/xml');
      }
    ],
    [
      'pattern' => 'sitemap',
      'action'  => function() {
        return go('sitemap.xml', 301);
      }
    ]
  ]
];

Merged:


<?php

return [
  'routes' => [
    [
      'pattern' => '(:any)',
      'action'  => function($uid) {
          $page = page($uid);
          if(!$page) $page = page('blog/' . $uid);
          if(!$page) $page = page('contact/' . $uid);
          if(!$page) $page = site()->errorPage();
          return $page;
      }
    ],
    [
      'pattern' => 'sitemap.xml',
      'action'  => function() {
          $pages = site()->pages()->index();
          // fetch the pages to ignore from the config settings,
          // if nothing is set, we ignore the error page
          $ignore = kirby()->option('sitemap.ignore', ['error']);
          $content = snippet('sitemap', compact('pages', 'ignore'), true);
          // return response with correct header type
          return new Kirby\Cms\Response($content, 'application/xml');
      }
    ],
    [
      'pattern' => 'blog/(:any)',
      'action'  => function($uid) {
          go($uid);
      }
    ],
    [
      'pattern' => 'contact/(:any)',
      'action'  => function($uid) {
          go($uid);
      }
    ],
    [
      'pattern' => 'sitemap',
      'action'  => function() {
        return go('sitemap.xml', 301);
      }
    ],
  ],
];

The two sitemap related routes should move to the top, before you call the (:any) pattern.

Great. Thank you!