Flash Messages
Overview
Heads Up! Flash messages require sessions. If you do not use the \Skinny\Middleware\SessionCookie middleware, you must start a native PHP session yourself.
Skinny supports flash messaging much like Rails and other larger web frameworks. Flash messaging allows you to define messages that will persist until the next HTTP request but no further. This is helpful to display messages to the user after a given event or error occurs.
As shown below, the Skinny application’s flash() and flashNow() methods accept two arguments: a key and a message. The key may be whatever you want and defines how the message will be accessed in the view templates. For example, if I invoke the Skinny application’s flash('foo', 'The foo message') method with those arguments, I can access that message in the next request’s templates with flash['foo'].
Flash messages are persisted with sessions; sessions are required for flash messages to work. Flash messages are stored in $_SESSION['skinny.flash'].
Next
The Skinny application’s flash() method sets a message that will be available in the next request’s view templates. The message in this example will be available in the template variable flash['error'].
<?php
$app->flash('error', 'User email is required');
Now
The Skinny application’s flashNow() method sets a message that will be available in the current request’s view templates. Messages set with the flashNow() application instance method will not be available in the next request. The message in the example below will be available in the template variable flash['info'].
<?php
$app->flashNow('info', 'Your credit card is expired');
Keep
This method tells the Skinny application to keep existing flash messages set in the previous request so they will be available to the next request. This method is helpful for persisting flash messages across HTTP redirects.
<?php
$app->flashKeep();