【问题标题】:Out of range page requests超出范围的页面请求
【发布时间】:2013-12-14 06:32:04
【问题描述】:

我是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


    【解决方案1】:

    paging 数据不可用要么是错误,要么是文档错误 - 我会说是前者,因为当 Paginator 组件已经这样做时再次计算记录有点多余.

    看负责代码:

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

    表明在params 数组中设置paging 键之前引发了异常。因此,在解决此问题之前,您要么必须修改核心,要么自己重新计算和计算,类似这样(可能需要一些调整):

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

    【讨论】:

    • 知道了!非常感谢!!
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      • 1970-01-01
      • 2016-08-18
      • 1970-01-01
      • 2012-12-15
      • 2018-02-04
      • 1970-01-01
      相关资源
      最近更新 更多