cakephp paginator helper shows error when delete t

2019-04-09 09:06发布

问题:

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


标签: cakephp-2.3