Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^238) CHAPTER 16 ■ ADVANCED ZEND FRAMEWORK
Listing 16-4. Registering a Custom View with the View Renderer
$view = new Zend_View();
$view->siteWideProperty = 'value';
$viewRenderer =
Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$viewRenderer->setView($view);
This is fairly straightforward, and you can do it during your bootstrapping process. Every
view script now has access to the $this->siteWideProperty value, which can be extremely useful
for setting paths to common locations, such as to your image, JavaScript, and CSS files.


Sharing Objects


The Zend_Registry component is designed to allow you to create objects and share them among
the various framework classes. This approach is similar to using global variables, but is less
likely to cause conflicts with other libraries you may be using in conjunction with the framework.
To store an object instance within Zend_Registry, you call the set() method and define a
name for the entry:

Zend_Registry::set('handle', $instance);

To retrieve this object at a later time, simply call the get() method:

$instance = Zend_Registry::get('handle');

Error Handling


In the previous chapter, you created a framework bootstrap that threw exceptions. This is not
the default behavior. You enabled it by calling throwExceptions(true) on the front controller
instance.
By default, the framework looks for a controller called ErrorController whenever a problem is
encountered, instead of throwing exceptions directly. It also sets a request parameter called
error_handler with the request object. This parameter contains details about the error condition.
To properly handle errors in your sample application, create a new controller class named
ErrorController and place it with your other controllers. In this controller, create a method
called errorAction, which will be invoked by the framework when an error condition occurs.
At this point, you should have a controller that looks like this:

<?php
class ErrorController extends Zend_Controller_Action {

public function errorAction() {
}

}

You will also need to create a view called error.phtml and place it in a directory called
error. The content of that view isn’t particularly important yet.

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

Free download pdf