Using View Helper for Accessing zend-expressive-flash Messages in Expressive 3
If you already tried zend-expressive 3.0.0alpha3, and want to use flash message for it, you can use the new component: “zendframework/zend-expressive-flash”.
Setup
We need to require the component and its dependencies:
$ composer require zendframework/zend-expressive-session:^1.0.0 \ zendframework/zend-expressive-session-ext:^1.0.0 \ zendframework/zend-expressive-flash:^1.0.0
After it, we can ensure that the ConfigProvider
for above components registered at config/config.php
:
$aggregator = new ConfigAggregator([ // ... \Zend\Expressive\Session\ConfigProvider::class, \Zend\Expressive\Session\Ext\ConfigProvider::class, \Zend\Expressive\Flash\ConfigProvider::class, // ... ]);
To make the flash messages available in all pages, we need to register it in the pipeline before RouteMiddleware
piping:
use Zend\Expressive\Flash\FlashMessageMiddleware; use Zend\Expressive\Session\SessionMiddleware; // ... $app->pipe(SessionMiddleware::class); $app->pipe(FlashMessageMiddleware::class); $app->pipe(RouteMiddleware::class);
Get the Messages via View Helper
Most of the use cases, we are displaying the flash messages in the layout. If we are using zend-view
for the template engine, we can create view helper to access the flash messages. What we need is a Zend\Expressive\Flash\FlashMessages
instance with passes Zend\Expressive\Session\Session
which passes $_SESSION
into it if session status is active, as follows:
use Zend\Expressive\Session\Session; use Zend\Expressive\Flash\FlashMessages; // ... $session = session_status() === PHP_SESSION_ACTIVE ? $_SESSION : []; $flashMessages = FlashMessages::createFromSession(new Session($session));
The messages
is a private property “currentMessages” which currently no public function that returns it. We can wait for the PR for it to be merge or use it via ReflectionProperty
or Closure
which can be called by method:
$flashMessages->getFlashes();
Knowing that, we can create the view helper as follows:
<?php // src/App/View\Helper\Flash.php declare(strict_types=1); namespace App\View\Helper; use Zend\Expressive\Session\Session; use Zend\Expressive\Flash\FlashMessages; use Zend\View\Helper\AbstractHelper; class Flash extends AbstractHelper { public function __invoke() : array { $session = session_status() === PHP_SESSION_ACTIVE ? $_SESSION : []; $flashMessages = FlashMessages::createFromSession( new Session($session) ); return $flashMessages->getFlashes(); } }
and register it into view_helpers
config at src/App/ConfigProvider::__invoke()
:
// ... public function __invoke() : array { return [ 'dependencies' => $this->getDependencies(), 'templates' => $this->getTemplates(), // register additional view helpers 'view_helpers' => [ 'invokables' => [ 'flash' => View\Helper\Flash::class, ], ], ]; } // ...
Accessing Flash Messages in Layout
In layout, we can now access it via invoked flash
view helper with loop the messages to be displayed:
<?php if ($flashes = $this->flash()) :?> <ul> <?php foreach($flashes as $message) : ?> <li><?php echo $message; ?></li> <?php endforeach; ?> </ul> <?php endif; ?>
Give it a try
You can now create a flash messages in some page and access it in next request in the layout, check the documentation for its usage!
[…] you already followed my 4 previous expressive posts, all requirements already […]
Thanks. It helped me a lot. I was struggling with the session retrieval in a custom flash view helper I was writing for this purpose.
I had to change the dependencies namings too (we are in 2020, and the mezzio project replaced zend/expressive) while implementing your solution.
Now I just have to customize the style of my flash messages.