Mohammed Alsharaf wrote a good working example; FlashMessenger is an action helper in Zend Framework used to pass messages for the users on the next request.
Thats all good, but what about presenting the messages for the users? how to store different type of messages, System, Error, or Success messages?
To achive my goal, i have built two classes, one is an action helper and the other one is view helper.
Action Helper Class
This action helper class act as a factory and singleton pattern. It store the messages in different namespaces of the flash messenger. You can also, retrieve a namesapce by its key to add another message.
/**
* @author Mohammed Alsharaf
* @category Core
* @package Core_ActionHelper
* @copyright Copyright (c) 2008-2009 Mohammed Alsharaf.
* @license http://framework.zend.com/license/new-bsd
*/
class Core_ActionHelper_Messenger extends Zend_Controller_Action_Helper_Abstract
{
protected $_flashMessenger = null;
public function messenger($name='error',$message=null) {
if($name == 'error' & $message === null) {
return $this;
}
if(!isset($this->_flashMessenger[$name])) {
$this->_flashMessenger[$name] = $this->getActionController() ->getHelper('FlashMessenger') ->setNamespace($name.'_message');
}
if($message !== null) {
$this->_flashMessenger[$name]->addMessage($message);
}
return $this->_flashMessenger[$name];
}
public function direct($name='error',$message=null) {
return $this->messenger($name,$message);
}
}
View Helper Class
The view helper loops all the namespaces of the flash messenger and render the messages using the htmlList view helper
/**
* @author Mohammed Alsharaf
* @category Core
* @package Core_ViewHelper
* @copyright Copyright (c) 2008-2009 Mohammed Alsharaf.
* @license http://framework.zend.com/license/new-bsd
*/
class Core_ViewHelper_Messenger extends Zend_View_Helper_Abstract
{
protected $_messageKeys = array(
'msg_message',
'error_message',
'info_message',
'success_message',
'warning_message',
);
public function messenger()
{
foreach($this->_messageKeys as $messageKey) {
if($messages = $this->_getMessages($messageKey)) {
echo $this->_renderMessage($messages,$messageKey);
}
}
}
protected function _getMessages($messageKey)
{
$result = array();
$flashMessenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
$flashMessenger->setNamespace($messageKey);
if($flashMessenger->hasMessages()) {
$result = $flashMessenger->getMessages();
}
// check view object
if(isset($this->view->$messageKey)) {
array_push($result, $this->view->$messageKey);
}
//add any messages from this request
if ($flashMessenger->hasCurrentMessages()) {
$result = array_merge($result,
$flashMessenger->getCurrentMessages()
);
//we don’t need to display them twice.
$flashMessenger->clearCurrentMessages();
}
return $result;
}
protected function _renderMessage($message, $name)
{
if(!is_array($message)) {
$message = array($message);
}
return $this->view->htmlList($message, false, array('class'=>$name), true);
}
}
Usage:
In your controller to add a message for the next request
// option one
$this->_helper->messenger('success',"Your message is here.");
// another option to add message
$this->_helper->messenger('success')->addMessage('Your message is here.');
To add a message in the current view:
$this->view->info_message = 'stiky message for the current view';
In your layout file add the following, so if there are messages to view, the helper will print them.
<div id="messages"><?php $this->messenger(); ?></div>
Update:
Carlton Gibson over at noumenal also have a very nice tutorial and view helper that you might want to look at.
http://blog.noumenal.co.uk/2009/08/using-zend-framework-flashmessenger.html
ZF FlashMessenger helper: http://blog.noumenal.co.uk/2009/08/using-zend-framework-flashmessenger.html