CakePHP pagination and filtering

Here’s another little trick in CakePHP - it’s quite often that someone will request a big old list of paginated data, along with filters on that data.

I’m not a massive fan of cakes named parameters (the “/key:value/” doodah in the url) - HTTP already has a method to do this - it’s called a query string, and I don’t see why they’ve tried to reinvent the wheel.

Anyway, I want to use query string parameters for my filters. The only issue with this is that the paginator helper then doesn’t remember the filters when you change page.

Here’s how to make sure all you’re query string parameters are carried over. Just before you add your pagination numbers or next/prev etc., do this:

$this->Paginator->options(array('url' => array('?' => $this->params['url'])));

That ugly-ol’ “unset” is there because for some reason unknown to me, Cake adds the current url in that array as if it where a query string parameter.

Of course you then chuck all this into an element with the rest of your pagination and never worry about it agin.