From The Blog

CakePHP: Custom routes and pagination

3 November 2012, in CakePHP

In CakePHP it is pretty easy to change the way that users reach to a specific action to another beautiful URL

for ex:

you may have a page that contains category details, and users reach to it via:

http://domain.com/categories/view/id

controller is "categories", action is "view", and it takes 1 parameter id

and you may want to have another form of URLs like

http://domain.com/category/category-name

and add a route to it in Config/routes.php

like this:

Router::connect('/category/:name', array('controller' => 'categories', 'action' => 'view'),array('pass'=>array('name')));

but if this view for this action has a pagination, you will observer that pagination links have the basic old URL, like "categories/view/id/page:2".

After investigation, and checking blogs/questions/..., I had a cool solution for it:

First: I have forgotten the extra parameters (pagination), for the new custom route I had created, so I fixed it to

Router::connect('/category/:name/*', array('controller' => 'categories', 'action' => 'view'),array('pass'=>array('name')));

Second: In the view file I have changed the default options for the paginator helper to be:

$this->Paginator->options(array('url'=> array(
'controller' => 'category',
'action' => $this->params['name'],
)));

and that's it, it worked perfectly :)



Post a Comment