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 […]

  • Google


    Below you’ll obtain the link to some internet sites that we think you ought to visit.

  • bahis siteleri

    …Additional Information ca be found here

    […]you made running a blog glance[…]

  • bahis siteleri


    […]I am now not sure where you are getting your info, however good topic.[…]

  • sportotobet

    …Visitor recommendations

    […]The overall look of your website is excellent, let neatly as the content material![…]

  • best building inspections

    best building inspections

    […]just beneath, are various totally not connected internet sites to ours, having said that, they are surely worth going over[…]

  • looking for sexy lingerie and sexy costumes

    looking for sexy lingerie and sexy costumes

    […]usually posts some quite interesting stuff like this. If you are new to this site[…]

  • hawaii building inspectors

    hawaii building inspectors

    […]one of our visitors not too long ago recommended the following website[…]

  • poker 88

    poker 88

    […]check beneath, are some completely unrelated internet sites to ours, however, they may be most trustworthy sources that we use[…]

  • video production services

    video production services

    […]we prefer to honor a lot of other internet internet sites around the net, even if they aren’t linked to us, by linking to them. Below are some webpages worth checking out[…]

  • MILF Porn

    MILF Porn

    […]always a major fan of linking to bloggers that I adore but do not get quite a bit of link appreciate from[…]

  • mp3juices


    […]one of our guests lately suggested the following website[…]

  • Google


    Check beneath, are some completely unrelated web-sites to ours, however, they are most trustworthy sources that we use.

  • nintendo dope

    nintendo dope

    […]please take a look at the internet sites we stick to, including this one, as it represents our picks in the web[…]

  • amazon bitcoin

    amazon bitcoin

    […]we came across a cool web site that you just could take pleasure in. Take a appear in case you want[…]

  • japanese fukui datumou

    japanese fukui datumou

    […]Here are several of the web pages we advise for our visitors[…]

  • japanese fukui datumou

    japanese fukui datumou

    […]Here is an excellent Weblog You may Come across Intriguing that we Encourage You[…]

  • Pc mac repair

    Pc mac repair

    […]Wonderful story, reckoned we could combine a number of unrelated data, nonetheless seriously worth taking a look, whoa did one particular understand about Mid East has got a lot more problerms at the same time […]

  • سئو وب سایت

    سئو وب سایت

    […]we came across a cool web site that you simply may appreciate. Take a appear in the event you want[…]

  • agencies for security guards

    agencies for security guards

    […]that could be the finish of this post. Right here you’ll come across some web-sites that we feel you’ll appreciate, just click the links over[…]

  • work at home 2018

    work at home 2018

    […]Sites of interest we’ve a link to[…]

  • datumoufukuiken


    […]the time to study or check out the content material or websites we have linked to below the[…]

  • betriebswirtschaftliche Beratung

    betriebswirtschaftliche Beratung

    […]the time to study or visit the content material or sites we have linked to beneath the[…]

  • Island News Hawaii

    Island News Hawaii

    […]one of our guests recently recommended the following website[…]

  • buy pure kona coffee

    buy pure kona coffee

    […]the time to read or go to the content material or websites we’ve linked to below the[…]

  • interracial porn

    interracial porn

    […]Sites of interest we’ve a link to[…]

  • pure gourmet coffee

    pure gourmet coffee

    […]although web-sites we backlink to beneath are considerably not associated to ours, we feel they’re truly worth a go by, so possess a look[…]

  • campervans campervans

    campervans campervans

    […]usually posts some quite fascinating stuff like this. If you are new to this site[…]

  • AC Blowing Agent

    AC Blowing Agent

    […]please check out the sites we stick to, including this a single, because it represents our picks from the web[…]

  • K-8 Private School

    K-8 Private School

    […]always a big fan of linking to bloggers that I really like but don’t get a good deal of link adore from[…]

  • Simeon-Yahweh


    […]although websites we backlink to beneath are considerably not related to ours, we feel they are really worth a go as a result of, so have a look[…]

  • texty pesen

    texty pesen

    […]Here is a great Weblog You may Obtain Exciting that we Encourage You[…]

  • The Best Benefits Of Yoga

    The Best Benefits Of Yoga

    […]we like to honor several other web internet sites on the net, even if they aren’t linked to us, by linking to them. Under are some webpages worth checking out[…]

Bake a comment

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

© daniel hofstetter. Licensed under a Creative Commons License