0
votes

Can the paginator be hi-jacked for view template navigation (ie. provide next/prev URLs)?

I currently have a paginator with 1 row per page. I wanted to modify the URLs to point to the current view, but with the row PK instead of the :page querystring parameter.

  1. First, visit model/view/5
  2. In the paginator URls:

    • Next link, would go to model/view/6
    • Previous link, would go to model/view/4
    • Paged links, would go to model/view/x

Is this possible? Can pagination URLs be rewritten in this manner?

1

1 Answers

0
votes

That should be possible, yes. First you need an appropriate route that makes use of an route element named page, like:

Router::connect('/model/view/:page', [
    'controller' => 'Models',
    'action' => 'view'
]);

This is required, as the paginator helper will set the page number via a key named page in the URL array when generating links. If you hadn't an element with that name in the route, the value would end in the query string. It is also important to not mark the page element as to be passed, as otherwise the value would slip into the URL arrays that the paginator helper generates, and that wouldn't match your route anymore.

The other thing you'd need to do, would be to make sure that the paginator component has access to the requested page number. By default the component reads the value from either the request query string variables, or from the pagination options. So, in the controller for example you could do something like:

$page = $this->request->param('page');
if ($page !== null) {
    $this->paginate['page'] = $page;
    // or
    $this->request->query['page'] = $page;
}

// ...

$result = $this->paginate();

For something more dry you could move this in a custom component, or an extended paginator component.

See also