Stephen's Thoughts, and Other Nonsense...

<?php echo $cleverComments[array_rand($cleverComments)]; ?>


Simple Zend Framework Action-View Messenger

This thought occured 4 years ago about Code, action-helper, messenger, view-helper & zend_framework.

Although Zend Framework comes with the Flash Messenger action helper, it isn't really all that useful as you need to send the messages manually into the view from your controller. When building a large application with a standard messenger, having to send the messages to the view in each controller is a bit of a waste of time.

To get around this, I have built a very simple pair of Action and View helpers which serve as a messenger.

Action Helper

 * Messenger Action Helper
 * Simple Helper for passing messages from the Action to the View
class Application_Controller_Helper_Messenger extends Zend_Controller_Action_Helper_Abstract
 * Stores the message in the session for future access
 * @param   String          $sMessage   Message title
 * @param   String          $sType      Message type/class
 * @param   Array|String    $aDetails   Array, or string, of extra message details
public function direct($sMessage, $sType = 'info', $aDetails = Array())
    // (1) Load the Session Namespace (use a unique namespace name!)
    $oSession = new Zend_Session_Namespace("Application_Messenger");

    // (2) Check the aMessages array exists in session
    if (!isset($oSession-&gt;aMessages))
        $oSession-&gt;aMessages = Array();

    // (3) Check $aDetails is an array of details
    if (!is_array($aDetails))
        $aDetails = Array($aDetails);

    // (4) Add Message to the Session aMessages array
    $oSession-&gt;aMessages[] = Array(
        'message'   =&gt; $sMessage,
        'type'      =&gt; $sType,
        'details'   =&gt; $aDetails,


Being a simple function we only use the direct() function, but you can add some extra functions for specific message types if you like.

Calling it within the Controller is a piece of cake:

$this->_helper->messenger("This is an error message", "error", Array('details #1', 'details#2'));

View Helper



  • Messenger View Helper
  • Simple Helper for passing messages from the Action to the View / class Application_View_Helper_Messenger extends Zend_View_Helper_Abstract { /*
    • Retrieve the messages and return to view

    • @param String $sType Only return specific type of messages

    • @return Array */ public function messenger($sType = null) { // (1) Load the Session Namespace (Use the SAME namespace as in the Action Helper!) $oSession = new Zend_Session_Namespace("Application_Messenger");

      // (2) Check the Messages array exists in Session if (!isset($oSession->aMessages)) $oSession->aMessages = Array();

      // (3) Loop through the messages within the Session $aReturn = Array(); foreach ($oSession->aMessages as $nId => $aMessage) { // (4) Check the current message type, if a specific type has been requested if (!is_null($sType) && $aMessage['type'] != $sType) continue;

       // (5) Add current message to return array
       $aReturn[] = $aMessage;
       // (6) Remove message from session, since they are only displayed once


      // (7) Return messages to view for display return $aReturn; } }

Again, it's a very simple helper with a single function. To display the messages in your Layout (orĀ  view), you just do the following:

<!-- Begin Messenger -->
<?php foreach ($this->messenger() as $aMessage): ?>
    <div class='<?php echo $aMessage['type']; ?>'>
            <strong><?php echo $aMessage['message']; ?></strong>
        <?php echo count($aMessage['details']) ? $this->htmlList($aMessage['details']) : ""; ?>
<?php endforeach; ?>
<!-- End Messenger -->

This code loops through the messages and uses the type as the class for CSS beautifying. The details are displayed in a list, if they are set.

That's about it. There we have a nice and simple Action-View messenger helper set for Zend Framework. Feel free to use it for whatever and change it as much as you like. (Oh, and if you have a brilliant idea for a change, let me know!)