Overriding CakePHP’s controller::flash() method

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.