cakephp paginator helper shows error when delete the last record from the last page

那年仲夏 提交于 2019-12-06 02:51:28

问题


I just want to redirect that to the last index if the last record is deleted from the last page. please help me to do this.

<?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:


As of CakePHP 2.3 - Out of Range page requests will throw an exception.

However the documentation is not correct in saying that the paging parameters will be available to you in $this->request->params['paging'], because those are defined after the exception is thrown. (This problem has been fixed in CakePHP 2.4.x two months ago)

So, to get the effect you want you can do something like this in your controller:

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


来源:https://stackoverflow.com/questions/21421935/cakephp-paginator-helper-shows-error-when-delete-the-last-record-from-the-last-p

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!