CakePHP分页路由

时间:2012-10-05 13:39:58

标签: php cakephp pagination

我的应用程序中有以下两条路由用于分页:

Router::connect('/news', array(
    'controller' => 'posts', 'action' => 'index','page' => 1
));

Router::connect('/news/page/:page*', 
    array('controller' => 'posts', 'action' => 'index'), 
    array('named' => array('page' => '[\d]+'))
);

第1页为/news,第2页为/news/page/2

它只是显示第一页......任何想法是什么问题?感谢

2 个答案:

答案 0 :(得分:1)

首先,如果您的操作接受正常参数,则不需要使用命名参数:

public function index($page = 1) {} // defaults to page 1

开箱即用,这将使以下网址有效:

/news ---------> NewsController::index(null); // defaults to page 1
/news/index/1 -> NewsController::index(1);
/news/index/2 -> NewsController::index(2);
etc.

现在只需添加一条路线,将/news/page/*映射到index操作,而不是page操作:

Router::connect('/news/page/*', array('controller' => 'news', 'action' => 'index'));

结果:

/news/page/2 -> NewsController::index(2);

答案 1 :(得分:0)

CakePHP有一个内置的PaginationComponent用于获取数据,PaginationHelper用于视图中的分页链接。

http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper

您无需为您的分页设置路线。

好的,如果您想拥有自定义路线,请将其更改为:

    Router::connect('/events', array('controller' => 'events', 'action' => 'index','page' => 1));
    Router::connect('/events/page/:page', array('controller' => 'events', 'action' => 'index'), array('page' => '[\d]+'));


    //find your data in params
    $this->request->params['page'];