Redirect with exit

Published on and tagged with cakephp  tip

If you do a redirect in CakePHP 1.1 you often have to add an exit() after the redirect statement to stop the script execution:

$this->redirect('controller/action');
exit();

Thanks to a small enhancement in CakePHP 1.2 in the form of an additional parameter for the redirect() function you can now do:

$this->redirect('controller/action', null, true);

It’s the same functionality as in the previous code snippet, but with the advantage that your code becomes testable (you cannot test a function which contains an exit(), as the exit() also stops the execution of the test).

18 comments baked

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License