2010 August

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->_helper->flashMessenger->addMessage('Task saved');
$this->_helper->redirector('index');

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)) : ?>
<ul id="messages">
<?php foreach ($this->messages as $message) : ?>
    <li><?php echo $this->escape($message); ?></li>
<?php endforeach; ?>
</div>
<?php endif; ?>

and that’s all there is to the FlashMessenger.

Author: Rob…

The Future of Zend Framework

As the Zend Framework community works on the final release for Zend Framework 1, version 1.11, due out in late September or October 2010, it is time to look to the future.

Version 2.0 of the Zend Framework will bring around some significant changes and 2.0 code will not be backward compatible with the 1.x branches. That isn’t to say that bringing a ZF 1.x application up to speed with ZF 2 is impossible, but it will take solme reworking and plenty of testing.

The primary goals of Zend Framework 2 include increasing overall performance of the framework, making components more consistent in their usage (standardizing constructor options, fluent interfaces all around, etc), and migrating to PHP namespaces.

The last point perhaps represents the biggest shift. Introduced in PHP 5.3 (5.3 will be the minimum version supported by ZF 2), namespaces are a way to simplify coding while keeping it logically separated into components. For example, using Zend Framework 1, you might use a component in the following way:

$db = new Zend_Db_Adapter_Pdo_Mysql($options);

The naming of Zend components is necessarily complex and can lead to long winded code. Namespaces allow you to tell PHP to use a particular namespace, so that any reference to a class can be greatly simplified. Using namespaces, the above code could be rewritten as:

namespace 'Zend/Db/Adapter/Pdo';
$db = new Mysql($options);

It may not look like much, but using namespaces can really increase the readability of your code.

When should we expect Zend Framework 2? Currently, development is in the earliest stages and many changes are still
being discussed by the community. Don’t expect a final, production ready release before Spring 2011.

In anticipation of the the upcoming 2.0 branch, I would recommend becoming familiar with namespaces and upgrading your server to PHP 5.3.

Author: nick
Source: ZendCoding

by News Robot on August 27, 2010 in News, No Comments »
tags: