Dec 15 2008
Using Proper CakePHP Redirects
(This is the third post in a series of posts on CakePHP tips.)
CakePHP has a handy helper to redirect users to another page, used in controllers:
$this->redirect(‘controller/action’)
Be careful: Apparently in CakePHP 1.1 and earlier this->redirect() doesn’t call exit() after the header redirect is set, so the PHP code after the redirect in your controller will get executed. Yikes! That’s a major security hole.
Happily, this issue is fixed in CakePHP 1.2, which defaults to calling exit after the redirect. Even so, the proper syntax for writing a CakePHP redirect is:
$this->redirect(‘controller/action’,null,true);
The second parameter lets you specify an HTTP response code to return with the redirect command. The last parameter (which defaults to true), specifies whether to call exit() after the redirect. Even though it defaults to true, you really should write your redirects this way.
thanks..nice info. can you explain little on how to redirect a page with parameters ie;
$this->redirect(controller, action, parameter)
i can redirect my pages to particular controller and action but there was problem with pointing it towards url parameters like a number
Thanks! Can you be more specific about your problem? You can just pass a single string to this->redirect to get moved to a specific page. I would also recommend asking CakePHP questions on stackoverflow.com.