Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 15 ■ INTRODUCTION TO THE ZEND FRAMEWORK^227

When visited, at /customers/redirect, this action should forward you to the index action.
You can access all helpers by their name using the getHelper() method of Zend_Controller_
Action.

The FlashMessenger Helper
The FlashMessenger helper is designed to store messages from one request to the next. It
allows you to add a message to be displayed on the next page, and it will handle all aspects of
the session, including removing the message from the session after it has been displayed.
The use of the FlashMessenger helper is demonstrated in Listings 15-11 and 15-12.

Listing 15-11. Using the FlashMessenger Helper (in CustomersController.php)

public function redirectAction() {
$this->getHelper('FlashMessenger')
->addMessage("This was set at the redirector");

$this->getHelper('redirector')->goto('show');
}

public function showAction() {
$this->view->messages = $this->getHelper('FlashMessenger')->getMessages();
}

Listing 15-12. A Message View (application//view/scripts/customers/show.phtml)

<?php foreach($this->messages as $message) { ?>
<?php echo $message; ?><br />
<?php } ?>

If you visit /customers/redirect, you should be redirected to /customers/show and see
your message. If you then reload the page, you will not see the message, because it was cleared
from the session as soon as it was shown.

Using Built-in View Helpers.


View helpers follow the same logic as action helpers, allowing you to create components that
are reusable for your views. You’ll learn about creating view helpers in Chapter 17. For now,
let’s look at the built-in helpers.
Many of the built-in view helpers assist with creating and binding forms to your views. The
forms created with helpers can be slightly wordier than normal; however, they do useful things
like output escaping, and they will make creating select drop-down lists from an array easier.
They can also help to provide a more consistent final output.
Since your customers table is a single column, using a view helper will be easy. Create a
new view as shown in Listing 15-13 and save it as /application/views/scripts/customers/
add.phtml.

McArthur_819-9C15.fm Page 227 Thursday, February 28, 2008 7:44 AM

Free download pdf