Comments: setFlash

By Finster on 24/10/08

1 - Using a custom layout

It's not clear in this documentation, but to use a custom layout, you can create a flash_message.ctp and pass "flash_message" as the layout.

Then, the layout should contain something like:

<div id="flashMessage" class="message"><? echo $content_for_layout ?></div>

Now, the really cool part is that anything you passed in the $params array gets set to a viewVars in the flash layout. So, I could have the following if I passed 'class'=>'agony' to the setFlash() call:

<div id="flashMessage" class="<? ife(isset($class), $class, "flash") ?>"><? echo $content_for_layout ?></div>

By adcworks on 8/12/08

2 - Don't forget the redirect

Use setFlash to mimic the Rails way of showing a flash message. That is, the form is submitted and the user is transferred back to the listings page for the model saved with a flash message. On successful save, use $this->Session->setFlash("your message") and don't forget to send the user to the listings page $this->redirect("/listing");