Search code examples
phpcakephppaginator

Cakephp retrieves pages over limit


I'm using cakephp 1.3 paginator, it works fine, except for this:

if I have 5 pages, accessing by this kind of url

http://example.com/blog/5

I'll get the last page, but if I access to

http://example.com/blog/10

I also get the last page. But I want to not retrieve any values, since is not a valid page.

My configuration is like this:

  var $paginate = array(
        'Post' => array(
            'limit' => 10,
            'order' => array('Post.id' => 'desc')           
        ) 
    );

$posts = $this->paginate('Post');

Solution

  • Well, that's unfortunately how it works until CakePHP 2.3, and it's buried in the middle of the Controller::paginate() method:

    https://github.com/cakephp/cakephp/blob/1.3.20/cake/libs/controller/controller.php#L1214-L1215

    // ...
    
    if ($page === 'last' || $page >= $pageCount) {
        $options['page'] = $page = $pageCount;
    
    // ...
    

    So you pretty much have only one option, calculate the page count yourself and use it to validate the current page.