Uploader

An all around general purpose file uploader for CakePHP. Packaged as a stand alone plugin with file validation, file scanning and support for a wide range of basic mime types.

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:

<?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>
?>
Related Entries:

3 Comments

10 / 2 = ?
Allowed: [code] [b] [i] [u]
  • Jason Hardy
    loudcreative.com
    Mar 17th 2009, 08:35
    1 have you had any experience with overriding the Form Validation Messages from the model?

    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:

    error('subnav')?>

    do you know how to override that in the controller?

    Thanks
  • Miles Johnson
    milesj.me
    Mar 17th 2009, 18:02
    2 @Jason Hardy - I'm not quite sure what your asking. Perhaps email me and ill go over it further.
  • bookseller
    libmaker.com
    Dec 2nd 2009, 21:40
    3 thanks ...
    i was looking for a comprehensive setFlash guide...this was quite helpful