Redirecting to a random subpage

Hello,
I’m new at using Kirby, and for my first project I looking for a way to create a /random link in my navigation that would redirect each time to a random page (from a choosen subpage list).

I don’t really know how to do that. Is that a rooter middleware thing?

Thanks in advance for your help :slight_smile:

You can do that with a route:

In your config:

return [
 'routes' => [
    [
      'pattern' => 'random',
      'action'  => function () {
        // get the random page here
        return go('path_to_random_page');
       }
    ],
  ]
];
1 Like

Okay thanks!
I managed to get a random subpage.
I don’t know if it’s the best way to do this but it’s works (still open to better solution)

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

],
'routes' => [
[
  'pattern' => 'random',
  'action'  => function () {
    $album = page('musics')->children()->shuffle()->first();
    return go($album->uri());
   }

],
1 Like

Okay, I just found this page, my fault. I would be happy to have it before, it would avoid me to create this topic:
https://getkirby.com/docs/cookbook/content-structure/random-content