Tixie
February 3, 2019, 12:19pm
1
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
texnixe
February 3, 2019, 12:52pm
2
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
Tixie
February 3, 2019, 1:34pm
3
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
Tixie
February 3, 2019, 2:50pm
5
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