A lightweight PHP library for handling flash messages with session storage & simple HTML rendering. This package does not depend on any framework, but it can be integrated with frameworks like Laravel, Symfony, CakePHP, and CodeIgniter if needed.
- Session-based flash message storage.
- Instant message rendering.
- Custom message types.
- Fully framework-agnostic.
The recommended way use Composer to install the package:
composer require nassiry/flash-messages
By default, the package stores messages in sessions to be displayed on the next page load.
require __DIR__ . '/vendor/autoload.php';
use Nassiry\FlashMessages\FlashMessages;
// Create an instance
$flash = FlashMessages::create();
// Add flash messages to be displayed on the next page load
$flash->success('Operation successful!');
$flash->error('An error occurred.');
// Render messages on the next page template file
$flash->render();
Note: Ensure that
session_start();
has been called in your script before using.
To display a message on the current page, set the second argument to true
(default is false
):
require __DIR__ . '/vendor/autoload.php';
use Nassiry\FlashMessages\FlashMessages;
// Create an instance
$flash = FlashMessages::create();
// Add flash messages to be displayed immediately
$flash->error('An error occurred.', true);
// Render messages instantly in the same page
$flash->render();
You can check if there are any stored messages:
if ($flash->hasMessages()) {
echo "There are flash messages available.";
}
You can retrieve all stored messages as an array:
$messages = $flash->getMessages();
foreach ($messages as $message) {
echo $message['type'] . ': ' . $message['message'] . "<br>";
}
Outputs all flash messages with default HTML structure
$flash->render();
<div class="flash flash-success">Operation successful!</div>
<div class="flash flash-error">An error occurred!</div>
To clear all stored messages:
$flash->clear();
Currently, the package support following message types.
$flash->success('Success message!');
$flash->error('Error message!');
$flash->info('Informational message!');
$flash->warning('Warning message!');
addCustomType(string $type, string $message, bool $instant = false)
$flash->addCustomType('notification',
'This is a custom notification message!',
true
);
$flash->addCustomType('alert',
'This is an alert message!',
false
);
If you need to customize the way messages are displayed, extend the FlashMessageRenderer
class:
use Nassiry\FlashMessages\FlashMessageRenderer;
class CustomRenderer extends FlashMessageRenderer
{
public function renderMessage(string $type, string $message): void
{
echo sprintf('<div class="custom-%s">%s</div>', htmlspecialchars($type), htmlspecialchars($message));
}
}
$renderer = new CustomRenderer();
$flash = new FlashMessages(new FlashMessageStorage(), $renderer);
$flash->success('Custom rendered message!');
$flash->render();
Run the unit tests to ensure the package works as expected:
Ensure you have all dependencies installed by running:
composer install
Execute the following command to run the test suite:
composer test
This package is open-source software licensed under the MIT license.
Contributions are welcome! Please feel free to submit a Pull Request or open an issue.