Route redirect to url with param

Hello :wave:

I’m trying to add a param to the URL on a certain condition. I have a url /books and based on the year when I visit, I eventually want a param added automatically to the URL.

Here’s my config:

  'routes' => [
    [
      'pattern' => '(:any)',
      'action' => function($slug) {
        if (str_ends_with($slug,"books")) {
          $yearNow =  date("Y");
          if (statusFair($yearNow) == "now") {
            return go('books'."/fairyear:".$yearNow);
          }
          $this->next();
        }
        $this->next();
      }
    ]

I get the right URL in the browser tab but then an error is displayed saying redirect isn’t working.

Looks like the redirect is going in loops. :thinking:
In deed, I checked and $slug doesn’t appear to catch params I’m afraid and whether I’m on /books or /books/fairyear:2024 its value is the same: books.

I can’t really do that with htaccess though. If anyone has a clue, I’d appreciate a lot. Been stuck on this for a while. :sweat_smile:

What’s the point of the redirect here? Why don’t you render the page with the params? Routes | Kirby CMS

Yes I could you use that way thanks @texnixe , but still : :any doesn’t catch params (from what I see when debugging). So I can’t really capture when it’s exactly domain.com/books and not domain.com/books/fairyear:2023 for example.

Or am I doing a mistake somewhere?

I tried that for example:

    [
      'pattern' => '(books/:any)',
      'action' => function ($value) {
          $data = [
            'fairyear' => $value,
          ];
          site()->logger()->debug($data);
          return page('books')->render($data);
      }
    ]

But it can’t detect anything domain.com/books/fairyear:2023

I think I’ll review the code where the logic for params is then. I just though it’d be more elegant to implement this condition directly at the route level and then let the logic in place do its work.