Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 16 ■ ADVANCED ZEND FRAMEWORK^251

Listing 16-16. Routing for a Name

$route2 = new Zend_Controller_Router_Route(
'/product/view/:name',
array(
'controller'=>'product',
'action'=>'viewByName',
'name'=>false
),
);

You will notice that this catchall route does not have a format and defaults the parameter
to a value of false. Because the first route took out all the IDs, anything left must be a name and
gets routed to viewByName instead. If you wanted to add a format regular expression, anything
that did not match would end up not being routed properly.

■Caution The order in which routes are added is important and will control which route is called. Adding
these routes in a backward order in the example here would result in the nullification of the numeric route.

Managing Sessions


Most readers will be familiar with working with sessions through the session_start() and
$_SESSION methodology. The Zend Framework introduces a new Zend_Session class that makes
working with sessions easier.
Zend_Session will handle starting the session for you, so you no longer need to call
session_start(). It also features a namespace option that allows you to separate all session
variables based on the component that is working with them. This lets you keep your variable
names simple, but still ensure that they won’t conflict with other parts of your application. Finally,
the framework offers the ability to set a session expiry in a number of “hops,” defined as the
number of times a session is started. For example, if you want to pass data from one page, redirect,
and load it on the next, you can set it to a single hop, and the information will be removed from
the session automatically after it is read on the next page.
Using Zend_Session is demonstrated in Listing 16-17.

Listing 16-17. Using Zend_Session (SessionController.php)

public function OneAction() {
$session = new Zend_Session_Namespace('AUniqueNamespace');
$session->setExpirationHops(1);
$session->key = 'value';

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

McArthur_819-9C16.fm Page 251 Friday, February 29, 2008 5:07 PM

Free download pdf