Using and understanding setFlash()
Monday, March 16th 2009, 1:06am
Topics: Tutorials, CakePHP
Tags: Controller, setFlash, Flash
Comments: 3
Permalink -
Tinylink
When I first started using CakePHP, I never used the Session->setFlash() method to display success or error messages. I never used it because I was under the impression that flash messages were automatically redirected if debug is turned off, but this only applies to the controllers flash(). So I started to use and understand setFlash(), and here are my findings. Heres an example of how I used to do success messages:
Now there isn't anything entirely wrong with the code above, it simply adds a bit of unnecessary logic. To do it correctly using Cake, you would remove all my instances of $pageMessage and instead use $this->Session->setFlash() (Session component must be active). This method saves the success message to the session, and within the view you would use $session->flash() to output the message.
By default the $session->flash() method will generate a layout for the flash message. You could use your own layout by creating a view in the app/views/layout/ folder. For example, if we created a layout called success.ctp, the code may look something like the following:
The $content_for_layout variable will be replaced with the flash message. And now to use this layout you would pass a second parameter with the name of the layout, like so:
You could use this technique and create as many flash layouts as you please, but there are other alternatives. The setFlash() can take a third parameter being an array of options/variables that can be used in the flash view. One of the default options is the class. You could set the class option to "success" and in the auto-generated view it will have the class success; you could then just create a .success class in your CSS.
What I presented is only a small way that you could use setFlash(). You could find more information on the setFlash() by visiting the link below. And with my closing statement, I will also throw up a quick example for using this method.
http://book.cakephp.org/view/400/setFlash
<?php // Controller action
function add($id) {
$pageMessage = '';
if (!empty($this->data)) {
if ($this->Post->save($this->data)) {
$pageMessage = 'Your post has been added!';
$this->data = '';
}
}
$this->pageTitle = 'Add Post';
$this->set('pageMessage', $pageMessage);
}
?>Now there isn't anything entirely wrong with the code above, it simply adds a bit of unnecessary logic. To do it correctly using Cake, you would remove all my instances of $pageMessage and instead use $this->Session->setFlash() (Session component must be active). This method saves the success message to the session, and within the view you would use $session->flash() to output the message.
By default the $session->flash() method will generate a layout for the flash message. You could use your own layout by creating a view in the app/views/layout/ folder. For example, if we created a layout called success.ctp, the code may look something like the following:
<div id="success"> Success! <?php echo $content_for_layout; ?> </div>
The $content_for_layout variable will be replaced with the flash message. And now to use this layout you would pass a second parameter with the name of the layout, like so:
$this->Session->setFlash('Your post has been added!', 'success');You could use this technique and create as many flash layouts as you please, but there are other alternatives. The setFlash() can take a third parameter being an array of options/variables that can be used in the flash view. One of the default options is the class. You could set the class option to "success" and in the auto-generated view it will have the class success; you could then just create a .success class in your CSS.
What I presented is only a small way that you could use setFlash(). You could find more information on the setFlash() by visiting the link below. And with my closing statement, I will also throw up a quick example for using this method.
http://book.cakephp.org/view/400/setFlash
<?php // controllers/posts_controller.php
function add($id) {
if (!empty($this->data)) {
if ($this->Post->save($this->data)) {
$this->Session->setFlash('Your post has been added!', 'success');
$this->data = '';
}
}
$this->pageTitle = 'Add Post';
}
// views/posts/add.ctp (somewhere in the code)
$session->flash();
// views/layouts/success.ctp
<div id="success">
Success!
<?php echo $content_for_layout; ?>
</div>
?>
3 Comments
loudcreative.com
Mar 17th 2009, 08:35
For example, if I set a default message for validation of the subnav field in the model... and I have the following in my view:
=$form->error('subnav')?>
do you know how to override that in the controller?
Thanks
milesj.me
Mar 17th 2009, 18:02
libmaker.com
Dec 2nd 2009, 21:40
i was looking for a comprehensive setFlash guide...this was quite helpful