2012-10-05 54 views
0

我在我的應用程序下面兩種途徑分頁:CakePHP的分頁路由

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和第2頁是/news/page/2

這只是表明一個頁面雖然..任何想法是什麼問題?謝謝

回答

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

是的,我知道。但我希望將它們的路由更改爲上面的文章而不是使用'/ news/page:2' – Cameron

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. 

現在只需添加到地圖路線到index行動,而不是page行動:

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

結果:

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