Rails-like Flash messages in CakePHP
Another thing lacking in CakePHP is it’s flash messaging mechanism. Right out of the box the flash() method in CakePHP displays your message on a stark page and then redirects to the page you want to go to. The nice thing about Ruby on Rails is that adding a flash message actually redirects you to your destination and then displays the message on that page. It’s much nicer, in my opinion.
Here’s a quick and easy way to get CakePHP’s flash() to act like Rails’ flash();
In app_controller.php put this method:
function flash($msg,$to){
$this->Session->setFlash($msg);
$this->redirect($to);
exit;
}
And in your layout add:
$session->flash();
Because we’re overriding the controller flash() method in app_controller.php you dont have to go and change any of your existing flash() calls.
Please view this article on the cake Bakery. it has this same methodology but adds some javascript highlighing and other stuff. This article has been removed from the cake bakery.
Recently
- 09.08 CakePHP default dateTime()
- 03.24 Extending CakePHP’s beforeFilter()
- 03.17 Rails-like Flash messages in CakePHP
- 03.08 CakePHP thumbnails with phpThumb
- 02.15 CakePHP model validation
- 02.10 CakePHP - saving dates in your model
- 09.19 Importing multiple pdf pages and documents into PDFlib - Part 2
- 09.18 Quick Link - Every CSS based layout you could want
- 09.04 Mongrel pid error fixed
- 09.04 Typo, Work in Progress