ZF2 FlashMessenger view helper

10th February 2013

The FlashMessenger is a great controller plugin to use in your projects. First time I used it with ZF2 I was looking for a simple way of being able to access the messages from my layout and my view.

After some Googling I was surprised to see that there wasn't a solid view helper out there that did that. So I made this with the bonus of using Twitter Bootstrap classes and the option of retrieving the current messages as well.

File: module/Application/Module.php

<?php

class Module {

    /* ... */

    public function getViewHelperConfig()
    {
        return array(
            'factories' => array(
                'flashMessages' => function($sm) {
                    $flashmessenger = $sm->getServiceLocator()
                        ->get('ControllerPluginManager')
                        ->get('flashmessenger');

                    $messages = new \Application\View\Helper\FlashMessages();
                    $messages->setFlashMessenger($flashmessenger);

                    return $messages;
                }
            ),
        );
    }
}

File: module/Application/src/Application/View/Helper/FlashMessages.php

<?php

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;
use Zend\Mvc\Controller\Plugin\FlashMessenger as FlashMessenger;

/**
 * @author Rick 
 * @company The Webmen
 */
class FlashMessages extends AbstractHelper
{
    /**
     * @var FlashMessenger
     */
    protected $flashMessenger;

    public function setFlashMessenger(FlashMessenger $flashMessenger)
    {
        $this->flashMessenger = $flashMessenger;
    }

    public function __invoke($includeCurrentMessages = false)
    {
        $messages = array(
            FlashMessenger::NAMESPACE_ERROR => array(),
            FlashMessenger::NAMESPACE_SUCCESS => array(),
            FlashMessenger::NAMESPACE_INFO => array(),
            FlashMessenger::NAMESPACE_DEFAULT => array()
        );

        foreach ($messages as $ns => &$m) {
            $m = $this->flashMessenger->getMessagesFromNamespace($ns);
            if ($includeCurrentMessages) {
                $m = array_merge($m, $this->flashMessenger->getCurrentMessagesFromNamespace($ns));
                $this->flashMessenger->clearCurrentMessagesFromNamespace($ns);
            }
        }

        return $messages;
    }
}

The usage is simple, from your layout or your view you can use this code to get the messages.

<?php foreach ($this->flashMessages() as $namespace => $messages) : ?>
    <?php if (count($messages)) : ?>
        <?php foreach ($messages as $message) : ?>
        <div class="alert alert-<?=$namespace?>">
            <?php echo $message; ?>
        </div>
        <?php endforeach; ?>
    <?php endif; ?>
<?php endforeach; ?>

You can also use $this->flashMessages(true) to include current messages (messages that are added on the same request).

<?php foreach ($this->flashMessages(true) as $namespace => $messages) : ?>

comments powered by Disqus