CakePHP 1.2 Flash Usage

Another short article today on something that took me some digging (but not as much as usual!) to learn. You see mention of setFlash() all the time when
looking for CakePHP info. Until now I never knew how to implement the view side of this properly. He is the simple example (the kind I like the best) for using flash.

Controller

$this->Session->setFlash('your email is coming');

view:

if ($session->check('Message.flash')) {
    $session->flash();
}

Note: You do not have to include Session in your component list in your controller for this to work. Also, you may want to place the $session->flash() code in your layout instead of in various views.

Happy Cake’n

Update

It was not clear to me how to use the flash method, but here it is:

$this->flash(‘The animal has been modified.’, ‘/find_a_pet/search’, 3);

The first parameter is the message.

The second parameter is the url to redirect the user.

The third parameter is the number of seconds before redirecting.

6 comments


  • A related thing which also took me a (shorter than usual as well) while to get, is the way to display the Auth->loginError message in a login form. The key used is not the default ‘Flash’ but ‘Auth’, hence the code to put in the form:
    check(‘Message.auth’)) $session->flash(‘auth’); ?>

    Voilà… happy coding !

    February 9, 2008
  • Ohhh!!!! Thankyou very much!!!!!!

    June 21, 2008
  • you can add that :
    var $helpers = array(‘Session’);

    in controller.

    June 21, 2008
  • Hey, cool tips. Perhaps I’ll buy a bottle of beer to the person from that forum who told me to go to your site :)

    April 14, 2009
  • I’m in the process of trying out CakePHP, but have some solid experience with Ruby on Rails. Things seem much less polished with Cake so far, and this whole flash stuff has been really painful compared to the elegance of Rails. Either-way, your post has definitely helped me, so thanks for that!

    Hopefully I’ll get my head around Cake soon enough…

    April 19, 2010
  • Another short article today on something that took me some digging to learn.thanks. :)

    October 11, 2010

Leave a comment


Name*

Email(will not be published)*

Website

Your comment*

Submit Comment

© Copyright Aaron Thies - Designed by Pexeto