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
<?PHP
/**
* 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->aMessages))
$oSession->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->aMessages[] = Array(
'message' => $sMessage,
'type' => $sType,
'details' => $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
<?php
/**
* 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
unset($oSession->aMessages[$nId]);
}
// (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']; ?>'>
<p>
<strong><?php echo $aMessage['message']; ?></strong>
</p>
<?php echo count($aMessage['details']) ? $this->htmlList($aMessage['details']) : ""; ?>
</div>
<?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!)
Tweet
Hi,
very interesting!
I’d like to use it , but get the following error:
“Fatal error: Uncaught exception ‘Zend_Loader_PluginLoader_Exception’ with message ‘Plugin by name ‘Messenger’ was not found in the registry; used paths:…”
Can you help me please?
I get an error:
Action Helper by name Messenger not found
can you please tell me what’s missing?
You need to set your config to look for the Helpers in the folders you’ve configured them in.
It’s been a while since I touched ZF1 so I don’t remember off the top of my head how to do it.
Thank you for this helpful article.
I had a little problem (not sure if this is only because of my project structure) but after changing the class names
Application_Controller_Helper_Messenger to Application_Controller_Action_Helper_Messenger and
Application_View_Helper_Messenger to Zend_View_Helper_Messenger
it worked like a charm.
Thanks for sharing,
Alex
No worries