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 is available at: https://docs.dotkernel.org/dot-flashmessenger/.
| 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.
Run this command in your project folder:
composer require dotkernel/dot-flashmessengerComposer also installs laminas/laminas-session, because the session handling uses this library.
Next, add the ConfigProvider to the configuration of your application.
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.
After the installation, the container has a FlashMessengerInterface service.
Inject this service into the classes that need flash messages.
$container->get(FlashMessengerInterface::class);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::SUCCESSEach 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;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.
Dot\FlashMessenger\FlashMessengerInterface::classThe flash messenger service
Dot\FlashMessenger\View\RendererInterface::classThe renderer service