cakephp paginator helper在删除最后一页的最后一条记录时显示错误

时间:2014-01-29 04:13:06

标签: cakephp-2.3

如果从最后一页删除最后一条记录,我只想将其重定向到最后一条索引。 请帮我这样做。

<?php                       
    echo $this->Paginator->prev
      ($this->Html->image('prev.png'), array('escape' => false), 
    array(), null, array('class' => 'prev'));
    echo $this->Paginator->counter
      ('Page {:page} of {:pages}, Total Records {:count}');                     
    echo $this->Paginator->next($this->Html->image
      ('next.png'), array('escape' => false), 
        array(), null, array('class' => 'next'));
 ?>

1 个答案:

答案 0 :(得分:3)

从CakePHP 2.3开始 - Out of Range page requests将抛出异常。

然而,在$ this-&gt; request-&gt; params ['paging']中可以使用分页参数时,文档不正确,因为在之后定义抛出异常。 (两个月前在CakePHP 2.4.x中已修复此问题

因此,要获得您想要的效果,您可以在控制器中执行以下操作:

public function index() {
    try {
        $paginatedData = $this->Paginator->paginate();
    } catch (NotFoundException $e) {
        //get current page
        $page = $this->request->params['named']['page'];
        if( $page > 1 ){
            //redirect to previous page
            $this->redirect( array( "page" => $page-1 ) );
        }else{
            $paginatedData = array(); //no data to paginate so use empty array()
                                      //you will have to check for this in the view and no longer display the pagination links, since they will NOT be defined
        }
    }
}