How do i resolve Error: The requested address was

2019-07-20 05:31发布

问题:

I'm using CakePHP 2.X in my project and stuck at one point.

First of all let you know that i implemented search functionality using form's POST Method, But for this i found error in pagination. Filter will not sustain for next page. So i changed form method to GET. now its working OK ( Not exactly what i required, All requested data displaying in URL ), but now i'm at the point where its create another issue.

I got below error when i trying to search anything ( Existing data in DB ), and go to next page using pagination, now i changed search keyword with not matched in DB ( Data Not Exist in DB ).

Error: The requested address was not found on this server

Which was not there at the time of POST method.

I have tried with all the option in debug mode (Configure::write('debug', 2)). but not getting any help from it.

Can anyone help me out from this? It will be really appreciated!

Thanks.

回答1:

The answer is simple.

lib/Cake/Controller/PaginatorComponent.php
if ($requestedPage > $page) { 
        throw new NotFoundException();
}

So just catch the error in your controller.

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

This is why: http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html#out-of-range-page-requests