Zend framework notification system

I was playing around with admin interface for one custom project and I wanted general notification system for updating, adding, deleting records in database or any other action I could imagine. Zend framework has built in notification system where you can easily call the following helper (it’s an controller action helper). I hope this code will help somebody.

$this->_helper->flashMessenger();

First you need to register your view variable in init() method if you want to have notifications in all view files of your controller.

public function init()
{
 
/* Initialize action controller here*/
$this->view->messages = $this->_helper->flashMessenger->getMessages();
 
}

Then you simply add messages in your action methods like this:

$this->_helper->flashMessenger->addMessage(array("ok_message" => 'record deleted'));
or
$this->_helper->flashMessenger->addMessage(array("err_message" => 'unable to comply'));

Why wasting you code when you got all in one place, right?

Once you register the message notification all you need is to choose where message should appear.That’s the easy part, just need to style your div ids and classes in view files.

< ?php if(isset($this->messages)):?>
<div class="grid_12">< ?php foreach($this->messages as $message):?>
<div id="site_info">
<div class="box">< ?php if(isset($message["error"])):?>< ?php echo $message["error"]?>
 
< ?php elseif(isset($message["ok"])):?>
 
< ?php echo $message["ok"]?>
 
< ?php endif?>
 
</div>
</div>
< ?php endforeach?>
 
</div>
< ?php endif?>

One trick, if you want to show them on all view pages, edit your layout file and paste the code above at header,footer or wherever you want them to be shown
(there is no need to put them in you view files, unless you want to show same notifications twice :)).

The easiest way to have notifications in other modules is to register it like session variable and unset them manually.

Check class Zend_Controller_Action_Helper_FlashMessenger for additional purposes.
Enjoy coding.