Custom Flash Messenger for Zend Framework

Mohammed Alsharaf wrote a good work­ing exam­ple; FlashMes­sen­ger is an action helper in Zend Frame­work used to pass mes­sages for the users on the next request.
Thats all good, but what about pre­sent­ing the mes­sages for the users? how to store dif­fer­ent type of mes­sages, Sys­tem, Error, or Suc­cess mes­sages?
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 fac­tory and sin­gle­ton pat­tern. It store the mes­sages in dif­fer­ent name­spaces of the flash mes­sen­ger. You can also, retrieve a name­sapce 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;

pub­lic func­tion messenger($name=‘error’,$message=null) {
if($name == ‘error’ & $mes­sage === 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];
}

pub­lic func­tion direct($name=‘error’,$message=null) {
return $this->messenger($name,$message);
}
}

View Helper Class

The view helper loops all the name­spaces of the flash mes­sen­ger and ren­der the mes­sages using the htm­l­List 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',
);

pub­lic func­tion mes­sen­ger()
{
foreach($this->_messageKeys as $mes­sageKey) {
if($messages = $this->_getMessages($messageKey)) {
echo $this->_renderMessage($messages,$messageKey);
}
}
}

pro­tected func­tion _getMessages($messageKey)
{
$result = array();
$flashMes­sen­ger = 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 mes­sages from this request
if ($flashMessenger->hasCurrentMessages()) {
$result = array_merge($result,
$flashMessenger->getCurrentMessages()
);
//we don’t need to dis­play them twice.
$flashMessenger->clearCurrentMessages();
}
return $result;
}

pro­tected func­tion _renderMessage($message, $name)
{
if(!is_array($message)) {
$mes­sage = array($message);
}
return $this->view->htmlList($message, false, array(‘class’=>$name), true);
}
}

Usage:

In your con­troller to add a mes­sage 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 mes­sage in the cur­rent view:

$this->view->info_message = 'stiky message for the current view';

In your lay­out file add the fol­low­ing, so if there are mes­sages to view, the helper will print them.

<div id="messages"><?php $this->messenger(); ?></div>

Update:
Carl­ton Gib­son over at noume­nal also have a very nice tuto­r­ial and view helper that you might want to look at.
http://blog.noumenal.co.uk/2009/08/using-zend-framework-flashmessenger.html

Tags: ,

1 Response to "Custom Flash Messenger for Zend Framework"

Leave a Comment

*

Get Adobe Flash player