CakePHP路由和分页

时间:2012-06-29 14:54:45

标签: cakephp pagination routes paginator

我创建了一个看起来像这样的路线

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

直到这里,一切正常,访问链接http://example.com/animals-and-pets,效果很好。

在这个页面上我有一个分页,这给我一个大问题,页面的链接产生了错误,如下所示:http://example.com/categories/view/animals-and-pets/page:2

我想要获得的结果是example.com/animals-and-pets/2

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我曾经这样做过: change CakePhp1.3 paginator destination url?

但是,如果您使用\page:2代替\2

,情况会更容易

现在在蛋糕2.0中,我调用$this->Paginator->options()在其余的分页选项之前在视图中设置正确的URL。类似的东西:

//Set the correct url for the pagination, cake will add the "page:" and "sort:" variables to this url
$this->Paginator->options(array('url'=> array('controller' => 'categories', 'action' => 'view', 'slug' => $this->params['pass'][0])));
//now display the pagination
echo $this->Paginator->counter(array('format' => __('Page {:page} of {:pages}')));
echo $this->Paginator->prev('«', array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next('»', array(), null, array('class' => 'next disabled'));

希望这有帮助