Route – redirect to automatically generated slug/pattern

Hi!

I have a structure field, where panel users can set category names for different categories (“pools”) to give each subpage of a page a category (the story behind that approach is another).

I then hook-update the structure field and create a slug entry (“poolUrl”) out of the category’s name (“displayName”) (see screenshot):

'page.update:after' => function($page) {
   if($page->resultPools()->exists()){
        $resultPools = $page->resultPools()->yaml();
             foreach($resultPools as &$resultPool) {
                  $resultPool["poolurl"] = strtolower(preg_replace('/[\W\s\/]+/', '-', $resultPool["displayname"]));
             }
        $page->update([
             'resultPools' => Data::encode($resultPools, "yaml")
        ]);
   }
}

My goal now is to create a route/virtual subpage where all of these automatically generated slugs redirect to the same page (“results.php”) or error page. On this page I would need the typed in slug as a value I can use as a variable.

So basically something like:

'pattern' => 'results/(:any)',
   'action'  => function () {
      return new Page([
         'slug' => 'results/ (or something?)', // <-- Hmmm, couldn't figure out what this does (despite studying the reference a lot)
         'template' => 'results',
         'content' => [
            'slug' => 'slug' // <-- the typed in slug/url which I can use on the results.php to filter certain pages, if the pattern doesn't fit any entry, show an error page or something. So maybe just the actual (:any) value from the pattern?
         ]
      ]);
   }

I studied the virtual pages and route reference pages a lot however, can anybody maybe point me in the right direction?

Of course I could just redirect “results/(:any)” to “results.php” and then php-get the url in the url bar and compare it to some other values, but that’s not really a real solution.

Thanks for any input!

If I understand this correctly, you want to render the results page but return additional data to that page, i.e. the (:any) part?

If so, then follow these examples: https://getkirby.com/docs/guide/templates/controllers#arguments-from-page-render-in-route

1 Like

Thanks a lot!