How to show flash messages in cakephp


Posted on 17 Nov 15 by Vickey

In every website we need to show success and failure messages. For example on login page or on sign up page we show some success or failure messages.Cakephp provide us  'setFlash' method.By using the 'setFlash' we can set messages in Controllers and can show these messages on our views

Set message in controller

$this->Session->setFlash(__('Please, try again.'));

Show on ctp file

<?php echo $this->Session->flash(); ?>

In some cases we need to give some class or name to a flash message so that we can give some css style to this message text

$this->Session->setFlash(__('Whoops! Please try again.', null), 'default', array('class' => 'error'));

We can set flash message by giving a different name

$this->Session->setFlash(__('Invalid username or password, Please try again.', null),  'default', array('class' => 'error'),'loginMessage');
<?php echo $this->Session->flash('loginMessage'); ?>


If this post help you reduce time to develop, you can help me to maintain this website :)