1
votes

I'm building a cakePHP app which uses pagination. At one point, I need the controller to check certain conditions, and, if they are met, to reset to the first of the paginated pages.

In other words, let's say the url is http://www.example.com/films/index/page:6

In the films controller, I have:

  function index() {
    if(conditions are met)
       $this->params['named']['page'] = 1;
  }

The idea being that if the conditions are met, then the paginator will show the first page of results rather than page 6.

I've checked that the conditions are being met, and the parameter ['named']['page'] is changed, but the paginator seems to be ignoring it and showing page 6 regardless.

What am I doing wrong?

Thanks!

1
you should always mention which cakephp version you are usingmark

1 Answers

3
votes

OK, there are two ways of doing this:

1 Changing the parameter: (You were close)

function index() {
    if(conditions are met)
       $this->request->params['named']['page'] = 1;
    $this->set('films', $this->paginate()); 
}

The problem with this solution is that the URL will remain the same (eg .../films/index/page:6) even when the user is really seing page 1, and it could be confusing.

2 Redirecting to the right page

function index() {
    if(conditions are met)
       $this->redirect(array('page' => 1));
    $this->set('films', $this->paginate()); 
}

Problem with this is that you have to do a redirection ;)

Hope it helps.