【问题标题】:CakePHP pagination routingCakePHP 分页路由
【发布时间】:2012-09-26 16:26:15
【问题描述】:

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

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]+'))
);

第一页是/news,第二页是/news/page/2

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

【问题讨论】:

    标签: php cakephp pagination


    【解决方案1】:

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

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

    开箱即用,这将使以下 URL 工作:

    /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);
    

    【讨论】:

      【解决方案2】:

      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'];
      

      【讨论】:

      • 是的,我知道。但我希望将它们的路由更改为像上面的帖子一样,而不是使用/news/page:2
      猜你喜欢
      • 2012-07-01
      • 2012-01-02
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 2012-06-27
      • 2013-12-20
      相关资源
      最近更新 更多