Lightweight, simple and easy way to display flash message with Bootstrap 5 (Alert) template
-
First of all, you can install via composer:
composer require elmyrockers/ezflash
-
Add the following line into your PHP code:
require_once 'vendor/autoload.php'; //Load Composer's autoloader use elmyrockers\EzFlash;
-
Create new instance of EzFlash class:
$message = new EzFlash;
-
After that, you can set flash message in 4 different ways:
$message->{$key} = {$yourmessage};
$message->success = 'Message'; //Property $message['success'] = 'Message'; //Array key $message->success( 'Message' ); //Method call or $message( 'success', 'Message' ); //Function call
-
Then, that flash message can be displayed using echo:
echo $message(); //Function call with no parameter
Or you can treat this object like a string:
echo $message;
For instance, if you write code like the following statement:
$message( 'success', 'My message' ); //Set flash message through function call echo $message; // Echo flash message (one-time display)
Then, its output will look like this one:
<div class="alert alert-success">My message</div>
-
You can set different template for each $key:
$message->setTemplate( ['success', 'danger', 'warning', 'info', 'primary', 'secondary', 'light', 'dark', 'default'], '<div class="alert alert-{$key}">{$message}</div>' ); //default //custom template $message->setTemplate( 'errorInfo', '<div class="alert alert-danger {$key}">{$message}</div>' );
You can override default/existing template too with this.