我正在构建一个使用分页的cakePHP应用程序。有一次,我需要控制器检查某些条件,如果满足,则重置为第一个分页页面。
换句话说,假设网址为http://www.example.com/films/index/page:6
在电影控制器中,我有:
function index() {
if(conditions are met)
$this->params['named']['page'] = 1;
}
这个想法是,如果符合条件,那么分页器将显示结果的第一页而不是第6页。
我已经检查过条件是否得到满足,参数['named'] ['page']被更改了,但是paginator似乎忽略了它并显示第6页。
我做错了什么?
谢谢!
答案 0 :(得分:3)
好的,有两种方法可以做到这一点:
1更改参数:(您已经关闭)
function index() {
if(conditions are met)
$this->request->params['named']['page'] = 1;
$this->set('films', $this->paginate());
}
此解决方案存在的问题是,即使用户确实在查看第1页,该网址仍将保持不变(例如.../films/index/page:6
),这可能会造成混淆。
2重定向到右页
function index() {
if(conditions are met)
$this->redirect(array('page' => 1));
$this->set('films', $this->paginate());
}
问题在于你必须进行重定向;)
希望它有所帮助。