Laraflash provides a handy way to work with the flash messages.
You can install this package via composer using this command:
composer require coderello/laraflash
After that you need to register the \Coderello\Laraflash\Middleware\HandleLaraflash::class
middleware after the \Illuminate\Session\Middleware\StartSession::class
one in the app\Http\Kernel.php
You can publish the config file with:
php artisan vendor:publish --tag="laraflash-config"
There are many syntax variations for adding flash messages, so you can choose the one you like the most.
Let's take a look at some of them.
use Coderello\Laraflash\Facades\Laraflash;
Laraflash::message()->content('Some content')->title('Some title')->type('success');
message()
method creates and returns freshFlashMessage
instance which can be modified by chaining methods (all methods could be found in theFlashMessage methods
section).
laraflash()->message()->content('Some content')->title('Some title')->type('success');
Laraflash
facade can be replaced with thelaraflash()
helper as you could saw in the example above.
laraflash()->message('Some content', 'Some title')->success();
message()
method accepts up to five arguments:$content
,$title
,$type
,$delay
,$hops
.
laraflash('Some content', 'Some title')->success();
Arguments mentioned in the previous example can be passed directly to the
laraflash()
helper.
Ready flash messages could be rendered using the render()
method of the Laraflash
instance.
laraflash()->render();
All methods of the
Laraflash
instance (which could be obtained by callinglaraflash()
helper without arguments being passed) could be found in theLaraflash methods
section.
Output HTML will be generated using skin, specified in the
laraflash.skin
config. All available skins are listed in the config file.
<div class="alert alert-danger" role="alert">
Danger message.
</div><br><div class="alert alert-info" role="alert">
Info message.
</div>
Default separator between the messages is the
<br>
, which is specified in thelaraflash.separator
config. Feel free to change it if you need.
Example of messages rendered as HTML:
Flash messages can be obtained as an array using the toArray()
method.
laraflash()->toArray();
Here is the result:
[
[
"title" => null,
"content" => "Instant message.",
"type" => "danger",
"hops" => 1,
"delay" => 0,
],
]
You can use array representation of flash messages for your API.
message(?string $content = null, ?string $title = null, ?string $type = null, ?int $delay = null, ?int $hops = null): FlashMessage
Creates and returns fresh FlashMessage
instance.
Renders ready flash messages as HTML.
Adds one more hop to each flash message.
Deletes all flash messages.
Returns the Collection
instance containing all flash messages.
Returns the Collection
instance containing ready flash messages.
Touches all flash messages (decrements amount of hops and delay, deletes expired messages).
Returns an array representation of ready flash messages.
Returns JSON representation of ready flash messages.
Sets the content of the flash message.
Sets the title of the flash message.
Sets the type of the flash message.
Sets the danger
type for the flash message.
Sets the warning
type for the flash message.
Sets the info
type for the flash message.
Sets the success
type for the flash message.
Sets the hops amount of the message (the number of requests in which the message will be present).
Default: 1
Sets the delay of the message (the number of requests in which the message will be waiting to receive the ready state).
Default: 1
Shortcut for ->delay(0)
Increments the amount of hops.
Sets the custom attribute which will be present in the array representation of the message and could be obtained using the get()
method.
Returns the value of the attribute.
Returns an array representation of the message.
Returns JSON representation of the message.
You can run the tests with:
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
Larflash is open-sourced software licensed under the MIT license.