超出范围的页面请求

问题描述 投票:0回答:2

我是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'],但没有显示任何内容,只是一个空值,我做错了什么?

请帮助我,思考

cakephp paginator cakephp-2.4
2个回答
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));
    }
}

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

    }
© www.soinside.com 2019 - 2024. All rights reserved.