CakePHP Pagination Recall Component

Posted by Matt on Thu, Jan 24 2008

I've been using the built in pagination in CakePHP 1.2-beta a lot lately and I must say it's freaking awesome. My one oh so minor grievance is that it doesn't "remember" where you were if you navigate off the page. For example many times I'll be paging through a sorted list of records and want to edit a particular one. After I edit and save I'm dropped back to the index with the default sorting on page 1.

I Googled around a bit and skimmed through the pagination code, but didn't see a quick way to turn this type of functionality on. So I wrote a quick component that keeps the sort/page in the session and re-applies it. This is all done automatically by just including the component in your controller.

You can download the component here.

I'll probably submit it to the CakePHP Bakery soon, but was hoping to get some feedback first.

Posted in CakePHP, Code

6 Comments

dr. Hannibal Lecter said on Jan 25, 2008
Hi there,

this is an interesting and useful component, thanks for sharing. It's amazing what you can accomplish with <50 lines of code using Cake. :-)

And a small suggestion: if you're proficient enough with the 1.2 pagination, why don't you post some HOWTOs for it? Examples: pagination, pagination w/ AJAX, pagination of a child model, pagination of a child model w/ AJAX etc.. I'm sure there are plenty of folks out there (myself included) that are not familiar enough with pagination to use it well. We would be very grateful :-)

Cheers!
Matt said on Jan 25, 2008
I've been pretty much just using the default settings for paginate. That's part of the reason I posted this here before submitting it to the Bakery. I'm not sure how well it works in the other scenarios your described.
Matt said on Feb 04, 2008
This code is now in the CakePHP Bakery:
http://bakery.cakephp.org/articles/view/pagination-recall
Cakechecker said on Mar 04, 2009
hi, this is of course a very important feature - I was frustrated to see cake pagination forgetting about the page last used. however - your code does not work with my cake installation? how do I have to use it? I am using an app that was auto generated with cake bake command. I put your code into app/controllers/components but it still does not remember the page. what went wrong? THANKS!
Cakechecker said on Mar 04, 2009
OK, I got it! For all he people new to cake terminology - you have to add this line to your controllers:
var $components = array('PaginationRecall');

Thanks for this useful example of extending cake. I was using this kind of behaviour long time in my "legacy" apps.