Zend Framework's Flash Messenger action helper
I've talked about Zend Framework's action helpers before, but haven't covered any of the action helpers that are supplied with Zend Framework.
FlashMessenger is a helper that allows you to store messages between requests. The most common use I have for it is for a "saved" message after doing an edit of an item that then redirects back to a list.
This is how it's used:
Storing a message
Storing to the FlashMessenger is easy. This is my typical usage within an action controller:
This code adds the message "Task saved" to the FlashMessenger and then redirects the user the index action, which in this case is a list of tasks. As should be obvious from the name of the method, you can add multiple messages and they will all be stored for retrieval after the next redirect.
The FlashMessenger will store the message that you've added for one hop, or number of requests. This means that the message will be available for retrieval on the next request, but unavailable on the request afterwards. This is very useful and it means that if someone refreshes the task list by hitting F5, then the "Task saved" message does not reappear.
Retrieving the stored messages
Retrieving the stored messages is similarly simple:
$this->view->messages = $this->_helper->flashMessenger->getMessages();
This will create an array of messages in your view object which you can then loop over in your view script:
<?php if (count($this->messages)) : ?>
<?php foreach ($this->messages as $message) : ?>
<li><?php echo $this->escape($message); ?></li>
<?php endforeach; ?>
< ?php endif; ?>
and that's all there is to the FlashMessenger.