A small change in Controller::redirect()

Published on and tagged with cakephp  controller  feature

If you wanted to stop the program execution after a redirect you had to use the code shown below up to now:

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

or

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

Thanks to a recent change of the API of the “redirect” method — the default value for the $exit parameter was changed from “false” to “true” — you can now simply use:

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

As it does an exit() by default, you can no longer encounter a possible security hole I described in Don’t forget to exit after a redirect (well, theoretically you can still encounter it when setting the $exit parameter explicitly to “false”, but that’s rather unlikely I think).

Merry Christmas to everyone!

PS: From the activity in the repository it looks like the Cake team is baking a Christmas Cake :)

7 comments baked

  • Yevgeny Tomenko

    I think you want to write
    “Thanks” to a recent change of the API…
    So migration to next version requeire to find and fix all redirects

  • Terr

    This is an excellent change, thanks for the heads up.

    Merry Christmas and a happy 2008. That you may continue to blog about cake in the next year.

  • cakebaker

    @Yevgeny, Terr: Thanks for your comments!

    @Yevgeny: Yes, if you don’t want the program execution to stop after a redirect then you have to find and fix those redirects. But I think in most cases you do not have to change anything as you usually want to stop the program execution after a redirect (at least up to now I never encountered another situation).

  • CakeFreak

    …let’s hope for the new year cake ;-)
    go cakeTeam go!
    Merry Xmas to all the bakers out there!
    Dan

  • Lobaman

    Merry xmas everyone.

    Damn, i just rewrite all my redirect()s to

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

    and yes, i learn to use
    $this->redirect(array(‘controller’=>’controller’,’action’=>’action’),null,true);

    anyway, very good job caketeam. Damn, im so excited to migrate to the newest version. :)

  • MN

    Merry Christmas cakebaker,
    I have never used a framework in php but just now I’m getting ready to start a new project. I searched for php frameworks and found cake one of the best known ones. But as my project is a little strange one, I was wondering if cake is a good choice.
    So I wanted to describe a little and ask you whether or not cake fits it or not:
    The most unusual part of project is that we have same models that should be organized in separate tables. I mean I have two tables named t_111 and t_222 for example, that have same structure but needed to be in two tables. Another thing is that these tables are created and managed dynamically.
    It would be very nice of you to tell me if cake is the right choice or not.

  • cakebaker

    @all: Thanks for your comments!

    @CakeFreak: It seems I was a bit too optimistic with the Christmas cake ;-)

    @MN: Well, I cannot tell you whether cake is the right choice for you, that’s something you have to decide yourself ;-) But what you described is not directly supported by cake. It should still be possible to realize it with cake, but it probably requires a bit more work. I recommend to write a small prototype to address those uncertainties.

Bake a comment




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

© daniel hofstetter. Licensed under a Creative Commons License