超出范围的页面请求

时间:2013-11-28 09:37:09

标签: cakephp paginator cakephp-2.4

我是cakephp的初学者,我的版本是2.4.3

在文档中我找到了下面的示例代码

public function index() {
    try {
        $this->Paginator->paginate();
    } catch (NotFoundException $e) {
        //Do something here like redirecting to first or last page.
        //$this->request->params['paging'] will give you required info.
    }
}

我的问题:

1.如何重定向到最后一页,有没有办法获得总页数?

2.我试图用debug()输出$ this-> request-> params ['paging'],但没有显示任何内容,只是一个空值,我做错了什么?

请帮助我,思考

2 个答案:

答案 0 :(得分:1)

在CakePHP 3.4上我有这个解决方案:

$page  = (!empty($this->request->query['pagina']) ? $this->request->query['pagina'] : 1);
    $this->paginate = ['page' => $page, 'limit' => '10'];

    try {

        $products = $this->paginate($products);

    } catch (NotFoundException $e) {

        $this->paginate = ['page' => $page-1, 'limit' => '10'];
        $products = $this->paginate($products);

    }

答案 1 :(得分:0)

paging数据不可用是一个错误,或文档错误 - 我会说它是前者,因为在Paginator时再次计算记录会有点多余组件已经做到了。

查看负责的代码:

https://github.com/cakephp/cakephp/blob/2.4.3/lib/Cake/Controller/Component/PaginatorComponent.php#L215

显示在paging数组中设置params键之前抛出异常。因此,在修复之前,你要么必须修改核心,要么自己重新计算和计算,这样的事情(可能需要一些调整):

public function index()
{
    try
    {
        $this->Paginator->paginate();
    }
    catch(NotFoundException $e)
    {
        extract($this->Paginator->settings);
        $count = $this->ModelName->find('count', compact('conditions'));
        $pageCount = intval(ceil($count / $limit));
        $this->redirect(array('page' => $pageCount));
    }
}