michel / flash
PHP Flash is a lightweight library for managing message flash functionality in PHP applications. It allows you to easily display temporary messages, such as success messages, warnings, and error notifications, to enhance the user experience.
Requires
- php: >=7.4
- michel/michel-package-starter: ^1.0
Requires (Dev)
- michel/unitester: ^1.0.0
This package is not auto-updated.
Last update: 2026-07-23 15:19:05 UTC
README
A simple, lightweight flash message manager for PHP.
Flash messages are temporary messages (like success, error, or warning notifications) that are stored in the session and automatically deleted after being read once.
It can be used as a standalone library in any PHP project, or natively integrated into the Michel Framework.
1. Installation
Install the package via Composer:
composer require michel/flash
2. Setup
Option A: Standalone PHP (Any Framework / Vanilla PHP)
To use it outside of the Michel Framework, just instantiate the Flash class and pass it your session array reference (usually $_SESSION).
<?php
session_start();
use Michel\Flash\Flash;
$flash = new Flash($_SESSION);
Option B: With Michel Framework
If you are using the Michel Framework, the package will auto-configure itself. Just add it to your config/packages.php file:
<?php
// config/packages.php
return [
\Michel\Flash\MichelPackage\MichelFlashPackage::class => ['dev', 'prod'],
];
(If you use the michel:setup wizard, this is done automatically!)
The Flash service is now automatically registered in the Dependency Injection Container. You can inject it directly into any controller.
3. Usage
The API is identical whether you use it standalone or via the framework.
Setting a Flash Message
The library provides convenient shortcut methods for common message types:
$flash->success('The post was created successfully!');
$flash->error('Something went wrong.');
$flash->warning('Please check your inputs.');
$flash->info('Your profile was updated.');
You can also use a custom message type with the set method:
$flash->set('custom_type', 'This is a custom message');
Retrieving Flash Messages
When you retrieve a message using get(), it is automatically deleted from the session. It returns the string message, or null if the message doesn't exist.
$successMessage = $flash->get('success');
if ($successMessage) {
echo "<div class='alert alert-success'>$successMessage</div>";
}
Keep it simple! ✨