Type of Flash Messenger in Zend

Mehmet Davut picture Mehmet Davut · Oct 16, 2010 · Viewed 9.4k times · Source

Is it possible or How can i give a type to a FlashMessage in Zend?

For example

/* This is a "Success" message */
$this -> _helper -> FlashMessenger('You are successfully created a post.'); 

/* This is an "Error" message  */
$this -> _helper -> FlashMessenger('There is an error while creating post.');

/* This is just a "Notification" message */
$this -> _helper -> FlashMessenger('Now you can see your Post');

Answer

rafeca picture rafeca · Oct 17, 2010

I think the best way to do this is by using the flashmessenger namespaces:

/* success message */
$this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Post created!');

/* error message */
$this->_helper->FlashMessenger()->setNamespace('error')->addMessage('You have no permissions');

And then in your layout you can get the messages added to each namespace:

<?php $flashMessenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');

<?php if ($flashMessenger->setNamespace('success')->hasMessages()): ?>
    <div class="message success">
    <?php foreach ($flashMessenger->getMessages() as $msg): ?>
        <?php echo $msg ?>
    <?php endforeach; ?>
    </div>
<?php endif; ?>

<?php if ($flashMessenger->setNamespace('error')->hasMessages()): ?>
    <div class="message error">
    <?php foreach ($flashMessenger->getMessages() as $msg): ?>
        <?php echo $msg ?>
    <?php endforeach; ?>
    </div>
<?php endif; ?>