dotkernel / dot-flashmessenger
Dotkernel flash messenger component for session messages between redirects
Requires
- php: ~8.3.0 || ~8.4.0 || ~8.5.0 || ~8.6.0
- laminas/laminas-session: ^2.21.0
- laminas/laminas-stdlib: ^3.20.0
- mezzio/mezzio-template: ^2.10.0 || ^3.0.0
- psr/container: ^1.1 || ^2.0
Requires (Dev)
- laminas/laminas-coding-standard: ^3.0.0
- phpstan/phpstan: ^2.1
- phpstan/phpstan-phpunit: ^2.0
- phpunit/phpunit: ^12.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-30 14:22:53 UTC
README
Flash messenger library for session messages between redirects. A flash message, or session message, is a piece of text that is available only in the next request. The library also keeps other session data, not only text messages, with the same behavior. With the flash messenger, you add data to the session and get it back in the next request. You do not set and clear the data yourself.
Documentation
Documentation is available at: https://docs.dotkernel.org/dot-flashmessenger/.
Badges
Versions
| Version | PHP | Documentation |
|---|---|---|
| 4.x | 8.3, 8.4, 8.5, 8.6 | docs/book/v4 |
| 3.x | 8.2, 8.3, 8.4, 8.5 | docs/book/v3 |
To upgrade from 3.x, read docs/book/v4/upgrading.md.
Installation
Run this command in your project folder:
composer require dotkernel/dot-flashmessenger
Composer also installs laminas/laminas-session, because the session handling uses this library.
Next, add the ConfigProvider to the configuration of your application.
Configuration
return [ 'dot_flashmessenger' => [ 'options' => [ 'namespace' => 'flash_messages', // Optional: a container service name, or a class that implements Laminas\Session\ManagerInterface. 'session_manager' => Laminas\Session\SessionManager::class, ], ], ];
namespace is the session namespace for all flash messages and data. The ConfigProvider sets it to dot_messenger.
Usage
After the installation, the container has a FlashMessengerInterface service.
Inject this service into the classes that need flash messages.
Getting the service in a factory
$container->get(FlashMessengerInterface::class);
Using the flash messenger service
A message that you add in a request is available only in the next request.
$this->flashMessenger->addMessage(FlashMessengerInterface::ERROR, 'This is an error flash message'); // In the next request, get the messages of one type, or of all types if you omit the type: $this->flashMessenger->getMessages(FlashMessengerInterface::ERROR); $this->flashMessenger->getMessages();
For data that is not a message, use addData() and getData() with a key:
$this->flashMessenger->addData('myData', $someData); // In the next request: $this->flashMessenger->getData('myData');
The interface has constants for the usual message types:
FlashMessengerInterface::ERROR FlashMessengerInterface::WARNING FlashMessengerInterface::INFO FlashMessengerInterface::SUCCESS
Each type has a shortcut method:
public function addError(string|array $error, string $channel = FlashMessengerInterface::DEFAULT_CHANNEL): void; public function addInfo(string|array $info, string $channel = FlashMessengerInterface::DEFAULT_CHANNEL): void; public function addWarning(string|array $warning, string $channel = FlashMessengerInterface::DEFAULT_CHANNEL): void; public function addSuccess(string|array $success, string $channel = FlashMessengerInterface::DEFAULT_CHANNEL): void;
FlashMessengerRenderer
This class turns the messages into HTML.
render() returns escaped HTML lists. renderPartial() renders your template with the messages, the service and the renderer.
dot-twigrenderer gives Twig functions that use this renderer.
Registered services
Dot\FlashMessenger\FlashMessengerInterface::class
The flash messenger service
Dot\FlashMessenger\View\RendererInterface::class
The renderer service