Removing filters (i.e. "page:") from URL via Routing

I was attempting to follow the last example in the documentation as seen below. My code is also below.

https://getkirby.com/docs/developer-guide/advanced/routing

c::set('routes', array(
    array(
        'pattern' => 'reviews/page:(:num)',
        
        'action' => function($page) {
            go('reviews/'.$page);
        }
    ),
    
    array(
        'pattern' => 'reviews/(:num)',
        
        'action' => function($page) {
            return site()->visit(page('reviews/page:'.$page));
        }
    )
));

Essentially I have pagination setup, so there’s URLs such as website.com/reviews/page:2 that I want to change to website.com/reviews/2. But when I try going to the URL right now, it directs me back to the homepage. If I put a error_log() command in the second array() statement, I do see $page is being passed through properly.

I searched on the forums, but the other posts appear to delve into issues that don’t involve numerical rewriting or the pagination like my issue does, so I’m a bit stumped. I have a feeling it’s a simple fix, but I’m not sure what the issue is.

page:2 is a so-called param, it’s not part of the page URI. You can’t currently set those params manually, but you can switch your pagination from the params method to the query method and use the following code in your second route:

$_GET['page'] = $page;
return site()->visit(page('reviews'));

So I’m using this now:

c::set('routes', array(
    array(
        'pattern' => 'reviews/?page=(:num)',
        
        'action' => function($page) {
            go('reviews/'.$page);
        }
    ),
    
    array(
        'pattern' => 'reviews/(:num)',
        
        'action' => function($page) {
            $_GET['page'] = $page;
            return site()->visit(page('reviews'));
        }
    )
));

But in regards to the pagination method switching you talked about, how do I do that? In my controller, I declare the pagination as such:

$pagination = $reviews->pagination();

Is there a flag I add here? I can’t seem to find anything in the documentation.

The following will probably not work because query params are not part of the pattern:

'pattern' => 'reviews/?page=(:num)'

You need to do this redirection from the reviews template.

You can set the method in the paginate() method:

$reviews->paginate($limit, ['method' => 'query']);

Ah, I see. I’ll note that for future reference then.

I ended up using a slightly alternate method (something similar to How to cleanup/remove the (tag:) from url), but I’ll keep this in mind in case I need it.

Apologizes for getting back to this post so late, been really busy with work and other IRL stuff and haven’t had much time to check things.

Hi Shawn, I run into this thread, because I am trying to achieve exactly the same thing regarding to pagination.

You mention you used a slightly altered method compared to the one mentioned in the thread about cleaning up the tag from the url. I have tried many things, but I cannot figure it out.

Please, could you elaborate on your solution. I would really like to hear (see some code) about how you managed this… that would be awesome.

I had to revert to the “stock” URL method unfortunately, and the code I had before got nuked due to my Git repo getting corrupted a while back and needing to be recreated.

So I don’t have the code I would have used back then anymore, sadly.

That’s a bummer :wink: But thanks for your fast reply.

I’ll try out some more… and if I can figure something out I’ll let you know here.