ZF2 Flash Messages

A consistent message and notification implementation is one of the key building stones of modern web apps. ZF2 makes it very easy to implement this with the FlashMessenger controller plugin.

To integrate the flash messenger into your web app you need to follow 3 simple steps.

First, register the FlashMessengers messages as a view variable in your Module.php. The FlashMessenger supports namespaces which is very useful for having different styling for different message contexts. This example uses 3 different namespaces. You are free to implement as many as you want. I’m merging the default namespace with the info namespace which lets you assign general messages without the need to specify a namespace.

public function onBootstrap(MvcEvent $e)
{
    $eventManager = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);

    // Show flashmessages in the view
    $eventManager->attach(MvcEvent::EVENT_RENDER, function($e) {
        $flashMessenger = new FlashMessenger;

        $messages = array();

        $flashMessenger->setNamespace('success');
        if ($flashMessenger->hasMessages()) {
            $messages['success'] = $flashMessenger->getMessages();
        }
        $flashMessenger->clearMessages();

        $flashMessenger->setNamespace('info');
        if ($flashMessenger->hasMessages()) {
            $messages['info'] = $flashMessenger->getMessages();
        }
        $flashMessenger->clearMessages();

        $flashMessenger->setNamespace('default');
        if ($flashMessenger->hasMessages()) {
            if (isset($messages['info'])) {
                $messages['info'] = array_merge($messages['info'], $flashMessenger->getMessages());
            }
            else {
                $messages['info'] = $flashMessenger->getMessages();
            }
        }
        $flashMessenger->clearMessages();

        $flashMessenger->setNamespace('error');
        if ($flashMessenger->hasMessages()) {
            $messages['error'] = $flashMessenger->getMessages();
        }
        $flashMessenger->clearMessages();

        $e->getViewModel()->setVariable('flashMessages', $messages);
    });
}

To have the messages display consistently throughout your application it is best to add the message HTML to the layout template (in most cases module/Application/view/layout/layout.phtml).

<?php
  if ($this->flashMessages) {
    foreach ($this->flashMessages as $context => $messages) {
      echo '<div class="message-container ' . $context . '"><ul>';
      foreach ($messages as $message) {
        echo '<li>' . $message . '</li>';
      }
      echo '</ul></div>';
    }
  }
?>

Finally you can assign messages in your controller. You don’t need to specify a namespace if you want to assign messages to the info group.

$flashMessenger = $this->flashMessenger();
$flashMessenger->setNamespace('success');
$flashMessenger->addMessage('Foo!');

I'm available for contracting work. Check out my LinkedIn profile and my portfolio page for an overview of my skills and experience. If you are interested in working with me please use the contact form to get in touch.

ZF2 Flash Messages

Leave a Reply

Your email address will not be published. Required fields are marked *