Berlioz FlashBag
, (*1)
Berlioz FlashBag is a PHP library to manage flash messages to showed to the user., (*2)
Installation
Composer
You can install Berlioz FlashBag with Composer, it's the recommended installation., (*3)
$ composer require berlioz/flash-bag
Dependencies
Usage
All messages are stored in session of user. So you be able to get the messages after a reload of page or redirect.
When you got the messages, they are deleted on the stack and no longer available., (*4)
Add message
It's very simple to add messages:, (*5)
$flashBag = new FlashBag;
$flashBag
->add(FlashBag::TYPE_SUCCESS, 'Message success')
->add(FlashBag::TYPE_INFO, 'Second message')
->add(FlashBag::TYPE_INFO, 'Third message for %d %s', 3, 'persons');
Some default types are available in constants:, (*6)
FlashBag::TYPE_INFO = 'info';
FlashBag::TYPE_SUCCESS = 'success';
FlashBag::TYPE_WARNING = 'warning';
FlashBag::TYPE_ERROR = 'error';
Get message
To get message, it's also simple then add:, (*7)
$flashBag = new FlashBag;
$successMessages = $flashBag->get('success');
foreach ($successMessages as $msg) {
print $msg;
}
Get all messages
You can also get all messages in one time:, (*8)
$flashBag = new FlashBag;
$allMessages = $flashBag->all();
foreach ($allMessages as $type => $messages) {
foreach ($messages as $msg) {
print sprintf('%s: %s', $type, $msg);
}
}