22 Mar 2010

CakePHP redirect destinations can sometimes be a little unexpected

In CakePHP controllers CRUD methods there is a lot of this as a way of reaching a default page:
$this->redirect(array('action'=>'index'));


It's okay for a small application, but when things start to get larger, like in a CMS, you'll find that the return path can be a little unexpected. To make everything more predictable, I now write this in the action method to which I want to return:
$this->Session->write('LastAdminPage', DS.$this->params['url']['url']);


And this wherever I need to make the return, like in a CRUD method on the controller or on a cancel button in the view:
$this->redirect($this->Session->read('LastAdminPage'));

No comments:

Post a Comment