[英]Routes pagination Cakephp 3
我正在使用CakePHP 3,我想分頁我的用戶。 但是,當我點擊第二頁時,URL看起來像/users?page=2
,我希望: /users/2
。
我在routes.php中創建了這條路線:
$routes->connect('/users/:page', ['controller' => 'users', 'action' => 'index'], ['page' => '[0-9]+']);
在“prev”按鈕之前的Users / index.ctp中我放了:
<?php
$this->Paginator->options([
'url' => [
'controller' => 'users',
'action' => 'index'
]
]);
?>
現在,當我點擊第2頁時, /users/2
打開,我收到此錯誤消息(RuntimeException):
Unable to locate an object compatible with paginate.
我錯過了什么或者我犯了什么錯誤嗎?
謝謝你的幫助。
PaginatorHelper內置了url格式,即使用?page = n。 它還會進行排序,例如用戶?page = 2&sort = user_id&direction = asc。 您的/ users / {page}格式不處理排序。
如果您真的想要堅持/ users / {page},那么您將不得不重寫PaginatorHelper。
嘗試使用paginator組件在控制器旁邊。 這個對我有用
$this->Paginator->paginate('Users')
對於自定義URL,請enter code here
需要實現索引操作
public function index($page = null){
$this->Paginator->settings = ['limit' => 15, 'page' => $page];
$this->set('users', $this->Paginator->paginate('Users'));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.