Welcome to Abdul Malik Ikhsan's Blog

Using View Helper for Accessing zend-expressive-flash Messages in Expressive 3

Posted in expressive, Zend Framework by samsonasik on February 11, 2018

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”.


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 \

After it, we can ensure that the ConfigProvider for above components registered at config/config.php:

$aggregator = new ConfigAggregator([
    // ...
    // ...

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;
// ...



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:


Knowing that, we can create the view helper as follows:

// src/App/View\Helper\Flash.php


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()) :?>
        <?php foreach($flashes as $message) : ?>
            <li><?php echo $message; ?></li>
        <?php endforeach; ?>
<?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!

2 Responses

Subscribe to comments with RSS.

  1. […] you already followed my 4 previous expressive posts, all requirements already […]

  2. Cyrille Nguetoum said, on September 20, 2020 at 4:19 pm

    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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: