Zend framework notification system

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.

You made it all the way down here so you must have enjoyed this post! You may also like:

Utilizing Magento notification system Petar Sambolek
Petar Sambolek, | 10

Utilizing Magento notification system

Using Redis cache backend and session storage in Magento Marko Martinovic
Marko Martinovic, | 43

Using Redis cache backend and session storage in Magento

Consuming Magento REST service using Zend_OAuth_Consumer Darko Goles
Darko Goles, | 45

Consuming Magento REST service using Zend_OAuth_Consumer

5 comments

  1. Vedran
    Simple dump of $this->message is
    array(1) { [0]=> array(1) { [“err_message”]=> string(14) “record deleted” } }
    where did you get $message[“error”]?

  2. You are right about Magento message system, but I made a post about Zend Framework and in latest library there is no such class as
    “Mage_Core_Model_Session_Abstract”.

    Thanks for the comment

  3. Isn’t the Magento message system the correct way to do this?
    Mage_Core_Model_Session_Abstract::
    addError
    addWarning
    addNotice
    addSuccess

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.