Routing works like expected, but how do I ignore the subpathes

Hi everybody,
I am working on a page with kirby and needed to redirect a parent path to a random subpath. That works pretty well, thanks to the help of a post in this forum.

The snippet of my config file looks like this:

'routes' => [
      [
        'pattern' => 'de/Schmuck',
        'action'  => function () {
          $albums = page('Schmuck')->children()->keys();
          return go($albums[array_rand($albums)]);
         }
      ],
      [
        'pattern' => 'en/Schmuck',
        'action'  => function () {
          $albums = page('Schmuck')->children()->keys();
          return go($albums[array_rand($albums)]);
         }
      ],
    ]

Unfortunately with this configuration every subpath which contains “de/Schmuck” links to a random child.
Example: mypath/de/Schmuck/category:Ringe redirects also to a random child of Schmuck. Is there any way to limit the pattern just to these specific path?

Thank you so much for you help! You’re support is awesome

You can use an if statement to check if the requested URL has parameters:

if ( empty ( kirby()->request()->params() ) ) {
  // do your thing
}
$this->next();

nice. Thank you for your fast response. How can I implement the if statement?

I tried it like this, but now the redirection doesn’t work at all:

'routes' => [
      [
        'pattern' => 'de/Schmuck',
        'action'  => function () {
          if ( empty ( kirby()->request()->params() ) ) {
            $albums = page('Schmuck')->children()->keys();
            return go($albums[array_rand($albums)]);
          }
          $this->next();  
         }
      ]
 ]

Ah, looks like we have to convert it into an array, try:

  if ( empty ( kirby()->request()->params()->toArray() ) ) {
            $albums = page('Schmuck')->children()->keys();
            return go($albums[array_rand($albums)]);
          }
1 Like

That’s it. Thank you so much!