Overriding CakePHP’s controller::flash() method

15Dec08

If you’re not satisfied with the way Cake’s flash method works, you can easily override it in your AppController class. At the moment, the default flash method takes you to a blank page only displaying the flash message. You are then redirected to the redirect url you provided. The way I prefer flash to work is to immediately redirect the user and show the flash message on that page. This can be easily achieved by by going into app_controller.php and adding the following couple of lines:

function flash($message, $url="/")
{
   $this->Session->setFlash($message);
   $this->redirect($url);
}

You will then need to modify your layout.ctp to display the flash message whenever it is set. To do this, you’ll need to add the following lines:

if ($session->check('Message.flash')) : $session->flash(); endif;

That’s it. No need to hack cake’s core files and no need to create a custom method when you can override the existing one.

Advertisement


No Responses Yet to “Overriding CakePHP’s controller::flash() method”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.