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:


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

  • Timo Derstappen

    Shouldn’t it be the other way round? Do you think there are that many cases where you redirect and not exit or is it just backwards compatibility?

    $this->redirect(‘controller/action’); # with exit
    $this->redirect(‘controller/action’, null, false); # without exit

    I’m too lazy to type ‘null, true’ everytime…

  • CakeBits » Redirect with exit

    […] cakebaker » Redirect with exit […]

  • Leech

    Nice tip.


    I’m lazy to write the odd ‘exit();’ each time I do a redirect.

  • Dieter@be

    Imho typing exit(); after a redirect isn’t very good.
    If you want to stop the current action before the redirect you can always use return(); This way other functions (like afterfilters, and just stuff that needs to be done in general) will be executed. If you want specific things not to happen then it’s a better approach to incorporate that in your business logic imho, and not “killing everything”

    Also keep in mind that the redirect function automatically disables $this->autoRender

  • cakebaker

    @Timo: Yes, it would be nicer the other way around. I think it was solved in the current way due to backward compatibility reasons.

    @Dieter: Yes, I agree with you. But you have to be careful with redirects in beforeFilters, as the action is still executed if you do a return in the beforeFilter.

  • Things you should know about cakePhp « Cake en Soda

    […] or return. Return would also be a good option but be careful when using it in the beforeFilter(). Read more here. Some more additional […]

  • Alex

    I agree with Timo.

    redirecting with exit should be default

  • Dieter@be

    @Daniel. Imho a beforeFilter is just meant as “filter”: I think it should just work *in service of* the real action, and not perform any drastic stuff on it’s own. I always let the beforeFilter “prepare” the real action by changing the state of your application (eg setting/changing some variables). The real action can detect this and take it from there (potentially doing drastic stuff like redirects).

    @Timo/Alex: I disagree: first of all i think api’s should be created as such that they make more sense instead of statistics of how they are used. (eg: if f(true) is called 60% of the time, and f(false) only 40% doesn’t mean true should be the default in the api). I think api’s should be defined so that functions by default do just the logic they are expected to do, and not incorporate logic (at least not by default if you can easily override it) that isn’t part of their domain. Also, I don’t think exit(); is that often used. Or at least it shouldn’t. Imho exit(); should only be used very very rarily. (as explained earlier)
    PS: you can always override the function with your own in AppController.

  • cakebaker

    @Dieter: Hm, in the cases I had to use a redirect in a beforeFilter it was for authentication purposes. And then it makes sense to do the redirect in the beforeFilter.

    Personally, I would expect that redirect() stops the script execution by default. With a redirect you do a new request and so it seems logical to me to finish the “old” request immediately. And it would help to prevent a possible security hole in your application (a security hole I had in my early cake apps).

  • CakePHP 1.2: redirect method at DLMax

    […] cakebaker on this too. Filed under: CakePHP […]

  • Darian Anthony Patrick

    Testing redirect() with exit() in PHP…

    The cakebaker mentions “…you cannot test a function which contains an exit(), as the exit() also stops the execution of the test…”.

    I wanted to share a testing method that I’ve been using in phpunit for testing non-CakePHP-centric code with f…

  • Ashutosh

    if($this->Estimate->Save($estimate_info)) {

    is not working its showing error

    Warning: Cannot modify header information – headers already sent by (output started at C:\Program Files\xampp\phpMyAdmin\projects\bethanypress\cake\basics.php:550) in C:\Program Files\xampp\phpMyAdmin\projects\bethanypress\cake\libs\controller\controller.php on line 447

    dont know whts wrong with this please give me some solution

    ***********I m waiting for your quick response**********

  • Ashutosh

    plz rply me on

  • cakebaker

    @Ashutosh: Well, such a warning usually means you output something before you do the redirect. So analyze the code whether there is for example an echo statement.

    Good luck!

  • Ivanoff

    In my case it was whitespaces at the end of models and controllers. Php thinks its the start of the page and then refuses to output anything. This is why the line indicated is at the end of the php files. Solution : delete any whitespace after ?>

  • cakebaker

    @Ivanoff: Ah, yes, that’s a common issue with PHP. To avoid it you can omit the ?>.

  • Missing Features » Using proper CakePHP redirects

    […] careful: Apparently in CakePHP 1.1 and earlier this->redirect() doesn’t call exit() after the header redirect […]

  • Recent URLs tagged Redirect - Urlrecorder

    […] recorded first by xzibrastripesx on 2008-12-16→ Comment on Redirect with exit by cakebaker […]

© daniel hofstetter. Licensed under a Creative Commons License