Cakephp pagination and custom named arguments handling

Let’s have a look at the scenario first.

I have a “contacts” controller, a “display” action in it(controller) and a view “display.ctp” to show results. As normal my “display” action lists all the contacts by calling $this->set(‘contacts’, $this->paginate(‘Contact’)) inside the controller action “display”. The view works normal.

Now, i want to pass a named argument to controller action. Say “category:friends” so that i could filter results further. In this case the calling url looks like “http://mysite.com/contacts/display/category:friends”. In general, when i call it, results span through multiple pages normally, but the html urls of “next”, “previous”, “1”, “2”, “3”..etc. no more have passed argument i.e. “category:friends” in it(the urls), rather it looks like http://mysite.com/contacts/display/page:1 as before. So what do i need to do so that pagination urls contain “category:friends” along with other pagination arguments like “page:1”, “sort:fieldname”, “direction:asc” etc. etc..? In other words i want pagination urls to look like http://mysite.com/contacts/display/page:1/category:friends and so on.

The answer is simple. Just add the following code anywhere in your view, but only before a call to paginator function:

$paginator->options(array('url' => $this->passedArgs));

In my case i use it like this:

$paginator->options(array('url' => $this->passedArgs));
echo $paginator->prev('< '); echo $paginator->numbers(array('separator'=>' '));
echo $paginator->next('>');

One thought on “Cakephp pagination and custom named arguments handling

Leave a Reply