2013-11-28 52 views
0

我在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.How到重定向到最后一页,有什么办法可以获得总页数?

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

请帮帮我,认为不是可用

回答

0

paging数据是一个错误,或者文件是错误的 - 我会说是前者,因为这将是有点多余的,当重新计数记录Paginator组件已经做到了。

望着负责代码:

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

显示,前pagingparams阵列中,在设定的异常被抛出。所以直到这个是固定的,你要么必须修改核心,或计数和对自己的重新计算,这样的事情(可能需要一些调整):

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

Got it!非常感谢你!! – Daruma

1

介绍CakePHP 3.x中,它适用于我。我希望帮助你。

try { 
     $this->paginate(); 
    } 
catch (NotFoundException $e) { 
     $page = $this->request->params['?']['page'] - 1; 
     return $this->redirect(['action' => 'index', 'page' => $page]); 
    }