Wallogit.com
2017 © Pedro Peláez
A simple PHP container for passing session (flash) messages between page redirects., (*1)
Install the FlashMessage package via Composer:, (*2)
composer require thecodemill/flash-message
FlashMessage is used to make messages of 4 different statuses:, (*3)
A message can be created by instantiating a new TheCodeMill\FlashMessage\Message class, with the appropriate arguments:, (*4)
use TheCodeMill\FlashMessage\Message;
$message = new Message('The problem with the gene pool is there’s no lifeguard', Message::STATUS_INFO);
Alternatively, static helper methods are available to automatically generate a message of each particular status:, (*5)
use TheCodeMill\FlashMessage\Message;
// Info
$info = Message::info('I have noticed something strange.');
// Warning
$warning = Message::warning('This could be a problem.');
// Danger
$danger = Message::danger('Yes, it was definitely a problem.');
// Success
$success = Message::success('All fixed!');
These messages can then be passed into session storage and flashed between page redirects., (*6)
Here's a Laravel example:, (*7)
Route::post('/submit', function () {
// Do something
// ...
// Now redirect
return redirect('/success')
->with('message', Message::success('Thanks for submitting.'));
});
To output the message, we may use the content() and status() methods on the Message object., (*8)
Note: You may notice that the status types tie in nicely with Bootstrap's default types., (*9)
Here's another Laravel Blade example:, (*10)
@if($message = session('message'))
<div class="alert alert-{{ strtolower($message->status()) }} alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert">
<span>×</span>
<span class="sr-only">Close</span>
</button>
{{ $message->content() }}
</div>
@endif