Zend Application

Getting an<wbr></wbr>MVC<wbr></wbr>applicationconfigured and ready to dispatch has required an increasing amountof code as more features become available: setting up the database,configuring your view and view helpers, configuring your layouts,registering plugins, registering action helpers, and more.

Additionally, you will often want to reuse the same code tobootstrap your tests, a cronjob, or a service script. While it'spossible to simply include your bootstrap script, oftentimes thereare initializations that are environment specific – you may notneed the<wbr></wbr>MVC<wbr></wbr>for a cronjob, orjust the DB layer for a service script.

Zend_Application<wbr></wbr>aims to make thiseasier and to promote reuse by encapsulating bootstrappinginto<wbr></wbr>OOP<wbr></wbr>paradigms.

Zend_Application<wbr></wbr>is broken intothree realms:

  • Zend_Application: loads the<wbr></wbr>PHP<wbr></wbr>environment,including include_paths and autoloading, and instantiates therequested bootstrap class.

  • Zend_Application_Bootstrap: provides interfaces forbootstrap classes.Zend_Application_Bootstrap_Bootstrap<wbr></wbr>providescommon functionality for most bootstrapping needs, includingdependency checking algorithms and the ability to load bootstrapresources on demand.

  • Zend_Application_Resource<wbr></wbr>provides aninterface for standard bootstrapping resources that can be loadedon demand by a bootstrap instance, as well as several defaultresource implementations.

Developers create a bootstrap class for their application,extendingZend_Application_Bootstrap_Bootstrap<wbr></wbr>orimplementing (minimally)Zend_Application_Bootstrap_Bootstrapper.The entry point (e.g.,<wbr></wbr>public/index.php) will loadZend_Application,and instantiate it by passing:

  • The current environment

  • Options for bootstrapping

The bootstrap options include the path to the file containing thebootstrap class and optionally:

  • Any extra include_paths to set

  • Any additional autoloader namespaces to register

  • Any<wbr></wbr>php.ini<wbr></wbr>settings toinitialize

  • The class name for the bootstrap class (if not "Bootstrap")

  • Resource prefix to path pairs to use

  • Any resources to use (by class name or short name)

  • Additional path to a configuration file to load

  • Additional configuration options

Options may be an array, a<wbr></wbr>Zend_Config<wbr></wbr>object,or the path to a configuration file.

Bootstrapping<wbr></wbr>

Zend_Application's second area of responsibility isexecuting the application bootstrap. Bootstraps minimally need toimplement<wbr></wbr>Zend_Application_Bootstrap_Bootstrapper,which defines the following<wbr></wbr>API:

  1. interface <wbr></wbr>Zend_Application_Bootstrap_Bootstrapper
  2. {
  3. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>__construct<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(0,0,255)">$application</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  4. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>setOptions<span style="color:rgb(102,204,102)">(</span><a href="http://www.php.net/array"><span style="color:rgb(0,0,102)">array</span></a><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$options</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  5. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>getApplication<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  6. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>getEnvironment<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  7. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>getClassResources<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  8. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>getClassResourceNames<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  9. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>bootstrap<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(0,0,255)">$resource</span><span><wbr></wbr></span>=<span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>null</strong></span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  10. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>run<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  11. }

This<wbr></wbr>API<wbr></wbr>allows thebootstrap to accept the environment and configuration from theapplication object, report the resources its responsible forbootstrapping, and then bootstrap and run the application.

You can implement this interface on your own, extendZend_Application_Bootstrap_BootstrapAbstract,or useZend_Application_Bootstrap_Bootstrap.

Besides this functionality, there are a number of other areas ofconcern you should familiarize yourself with.

Resource Methods<wbr></wbr>

The<wbr></wbr>Zend_Application_Bootstrap_BootstrapAbstract<wbr></wbr>implementationprovides a simple convention for defining class resource methods.Any protected method beginning with a name prefixedwith<wbr></wbr>_init<wbr></wbr>willbe considered a resource method.

To bootstrap a single resource method, use the<wbr></wbr>bootstrap()<wbr></wbr>method,and pass it the name of the resource. The name will be the methodname minus the<wbr></wbr>_init<wbr></wbr>prefix.

To bootstrap several resource methods, pass an array of names. Toobootstrap all resource methods, pass nothing.

Take the following bootstrap class:

  1. class <wbr></wbr>Bootstrap <wbr></wbr> extends <wbr></wbr>Zend_Application_Bootstrap_Bootstrap
  2. {
  3. <wbr><wbr> protected<span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>_initFoo<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span></wbr></wbr>
  4. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">{</span></wbr></wbr>
  5. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(128,128,128)"><em>//...</em></span></wbr></wbr></wbr></wbr>
  6. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">}</span></wbr></wbr>
  7. <wbr></wbr>
  8. <wbr><wbr> protected<span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>_initBar<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span></wbr></wbr>
  9. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">{</span></wbr></wbr>
  10. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(128,128,128)"><em>//...</em></span></wbr></wbr></wbr></wbr>
  11. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">}</span></wbr></wbr>
  12. <wbr></wbr>
  13. <wbr><wbr> protected<span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>_initBaz<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span></wbr></wbr>
  14. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">{</span></wbr></wbr>
  15. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(128,128,128)"><em>//...</em></span></wbr></wbr></wbr></wbr>
  16. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">}</span></wbr></wbr>
  17. }

To bootstrap just the<wbr></wbr>_initFoo()<wbr></wbr>method,do the following:

  1. $bootstrap-> bootstrap ( 'foo' );

To bootstrap the<wbr></wbr>_initFoo()<wbr></wbr>and<wbr></wbr>_initBar()<wbr></wbr>methods,do the following:

  1. $bootstrap-> bootstrap ( array ( 'foo', <wbr></wbr> 'bar' ) );

To bootstrap all resource methods, call<wbr></wbr>bootstrap()<wbr></wbr>withno arguments:

  1. $bootstrap-> bootstrap ( );

Bootstraps that use resource plugins<wbr></wbr>

To make your bootstraps more re-usable, we have provided theability to push your resources into resource plugin classes. Thisallows you to mix and match resources simply via configuration. Wewill cover<wbr></wbr>howto create resources<wbr></wbr>later; in thissection we will show you how to utilize them only.

If your bootstrap should be capable of using resource plugins, youwill need to implement an additional interface,<wbr></wbr>Zend_Application_Bootstrap_ResourceBootstrapper.This interface defines an<wbr></wbr>APIfor locating, registering, and loadingresource plugins:

  1. interface <wbr></wbr>Zend_Application_Bootstrap_ResourceBootstrapper
  2. {
  3. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>registerPluginResource<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(0,0,255)">$resource</span>,<span><wbr></wbr></span><span style="color:rgb(0,0,255)">$options</span><span><wbr></wbr></span>=<span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>null</strong></span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  4. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>unregisterPluginResource<wbr><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(0,0,255)">$resource</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr></wbr>
  5. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>hasPluginResource<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(0,0,255)">$resource</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  6. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>getPluginResource<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(0,0,255)">$resource</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  7. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>getPluginResources<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  8. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>getPluginResourceNames<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  9. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>setPluginLoader<span style="color:rgb(102,204,102)">(</span>Zend_Loader_PluginLoader_Interface<span style="color:rgb(0,0,255)">$loader</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  10. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>getPluginLoader<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  11. }

Resource plugins basically provide the ability to create resourceintializers that can be re-used between applications. This allowsyou to keep your actual bootstrap relatively clean, and tointroduce new resources without needing to touch your bootstrapitself.

Zend_Application_Bootstrap_BootstrapAbstract<wbr></wbr>(andZend_Application_Bootstrap_Bootstrap<wbr></wbr>byextension) implement this interface as well, allowing you toutilize resource plugins.

To utilize resource plugins, you must specify them in the optionspassed to the application object and/or bootstrap. These optionsmay come from a configuration file, or be passed in manually.Options will be of key to options pairs, with the key representingthe resource name. The resource name will be the segment followingthe class prefix. For example, the resources shipped with ZendFramework have the class prefix "Zend_Application_Resource_";anything following this would be the name of the resource. As anexample,

  1. $application <wbr></wbr>= <wbr></wbr> new <wbr></wbr>Zend_Application (APPLICATION_ENV, <wbr></wbr> array (
  2. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(255,0,0)">'resources'</span><span><wbr></wbr></span>=&gt;<span><wbr></wbr></span><a href="http://www.php.net/array"><span style="color:rgb(0,0,102)">array</span></a><span style="color:rgb(102,204,102)">(</span></wbr></wbr>
  3. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(255,0,0)">'FrontController'</span><span><wbr></wbr></span>=&gt;<span><wbr></wbr></span><a href="http://www.php.net/array"><span style="color:rgb(0,0,102)">array</span></a><span style="color:rgb(102,204,102)">(</span></wbr></wbr></wbr></wbr>
  4. <wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(255,0,0)">'controllerDirectory'</span><span><wbr></wbr></span>=&gt;APPLICATION_PATH .<span><wbr></wbr></span><span style="color:rgb(255,0,0)">'/controllers'</span>,</wbr></wbr></wbr></wbr></wbr></wbr>
  5. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">)</span>,</wbr></wbr></wbr></wbr>
  6. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">)</span>,</wbr></wbr>
  7. ) );

This indicates that the "FrontController" resource should be used,with the options specified.

If you begin writing your own resource plugins, or utilizethird-party resource plugins, you will need to tell your bootstrapwhere to look for them. Internally, the bootstraputilizesZend_Loader_PluginLoader,so you will only need to indicate the common class prefix an pathpairs.

As an example, let's assume you have custom resource pluginsin<wbr></wbr>APPLICATION_PATH/resources/and that they share thecommon class prefix of<wbr></wbr>My_Resource.You would then pass that information to the application object asfollows:

  1. $application <wbr></wbr>= <wbr></wbr> new <wbr></wbr>Zend_Application (APPLICATION_ENV, <wbr></wbr> array (
  2. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(255,0,0)">'pluginPaths'</span><span><wbr></wbr></span>=&gt;<span><wbr></wbr></span><a href="http://www.php.net/array"><span style="color:rgb(0,0,102)">array</span></a><span style="color:rgb(102,204,102)">(</span></wbr></wbr>
  3. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(255,0,0)">'My_Resource'</span><span><wbr></wbr></span>=&gt;APPLICATION_PATH .<span><wbr></wbr></span><span style="color:rgb(255,0,0)">'/resources/'</span>,</wbr></wbr></wbr></wbr>
  4. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">)</span>,</wbr></wbr>
  5. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(255,0,0)">'resources'</span><span><wbr></wbr></span>=&gt;<span><wbr></wbr></span><a href="http://www.php.net/array"><span style="color:rgb(0,0,102)">array</span></a><span style="color:rgb(102,204,102)">(</span></wbr></wbr>
  6. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(255,0,0)">'FrontController'</span><span><wbr></wbr></span>=&gt;<span><wbr></wbr></span><a href="http://www.php.net/array"><span style="color:rgb(0,0,102)">array</span></a><span style="color:rgb(102,204,102)">(</span></wbr></wbr></wbr></wbr>
  7. <wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(255,0,0)">'controllerDirectory'</span><span><wbr></wbr></span>=&gt;APPLICATION_PATH .<span><wbr></wbr></span><span style="color:rgb(255,0,0)">'/controllers'</span>,</wbr></wbr></wbr></wbr></wbr></wbr>
  8. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">)</span>,</wbr></wbr></wbr></wbr>
  9. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">)</span>,</wbr></wbr>
  10. ) );

You would now be able to use resources from that directory.

Just like resource methods, you use the<wbr></wbr>bootstrap()<wbr></wbr>methodto execute resource plugins. Just like with resource methods, youcan specify either a single resource plugin, multiple plugins (viaan array), or all plugins. Additionally, you can mix and match toexecute resource methods as well.

  1. //Execute one:
  2. $bootstrap-> bootstrap ( 'FrontController' );
  3. <wbr></wbr>
  4. //Execute several:
  5. $bootstrap-> bootstrap ( array ( 'FrontController', <wbr></wbr> 'Foo' ) );
  6. <wbr></wbr>
  7. //Execute all resource methods and plugins:
  8. $bootstrap-> bootstrap ( );

Resource Registry<wbr></wbr>

Many, if not all, of your resource methods or plugins willinitialize objects, and in many cases, these objects will be neededelsewhere in your application. How can you access them?

Zend_Application_Bootstrap_BootstrapAbstract<wbr></wbr>provides a localregistry for these objects. To store your objects in them, yousimply return them from your resources.

For maximum flexibility, this registry is referred to as a"container" internally; its only requirements are that it is anobject. Resources are then registered as properties named after theresource name. By default, an instance of<wbr></wbr>Zend_Registry<wbr></wbr>isused, but you may also specify any other object you wish. Themethods<wbr></wbr>setContainer()<wbr></wbr>and<wbr></wbr>getContainer()<wbr></wbr>maybe used to manipulate the container itself.getResource($resource)<wbr></wbr>canbe used to fetch a given resource from the container,andhasResource($resource)<wbr></wbr>tocheck if the resource has actually been registered.

As an example, consider a basic view resource:

  1. class <wbr></wbr>Bootstrap <wbr></wbr> extends <wbr></wbr>Zend_Application_Bootstrap_Bootstrap
  2. {
  3. <wbr><wbr> protected<span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>_initView<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span></wbr></wbr>
  4. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">{</span></wbr></wbr>
  5. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$view</span><span><wbr></wbr></span>=<span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>new</strong></span><span><wbr></wbr></span>Zend_View<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr></wbr></wbr>
  6. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(128,128,128)"><em>// moreinitialization...</em></span></wbr></wbr></wbr></wbr>
  7. <wbr></wbr>
  8. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(177,177,0)">return</span><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$view</span>;</wbr></wbr></wbr></wbr>
  9. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">}</span></wbr></wbr>
  10. }

You can then check for it and/or fetch it as follows:

  1. //Using the has/getResource() pair:
  2. if <wbr></wbr> ( $bootstrap-> hasResource ( 'view' ) ) <wbr></wbr> {
  3. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$view</span><span><wbr></wbr></span>=<span><wbr></wbr></span><span style="color:rgb(0,0,255)">$bootstrap</span>-&gt;<span style="color:rgb(0,102,0)">getResource</span><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(255,0,0)">'view'</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  4. }
  5. <wbr></wbr>
  6. // Viathe container:
  7. $container <wbr></wbr>= <wbr></wbr> $bootstrap-> getContainer ( );
  8. if <wbr></wbr> ( isset ( $container-> view ) ) <wbr></wbr> {
  9. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$view</span><span><wbr></wbr></span>=<span><wbr></wbr></span><span style="color:rgb(0,0,255)">$container</span>-&gt;<span style="color:rgb(0,102,0)">view</span>;</wbr></wbr>
  10. }

Please note that the registry and also the container is not global.This means that you need access to the bootstrap in order to fetchresources.<wbr></wbr>Zend_Application_Bootstrap_Bootstrap<wbr></wbr>providessome convenience for this: during its<wbr></wbr>run()<wbr></wbr>execution,it registers itself as the front controller parameter "bootstrap",which allows you to fetch it from the router, dispatcher, plugins,and action controllers.

As an example, if you wanted access to the view resource from abovewithin your action controller, you could do the following:

  1. class <wbr></wbr>FooController <wbr></wbr> extends <wbr></wbr>Zend_Controller_Action
  2. {
  3. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>init<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span></wbr></wbr>
  4. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">{</span></wbr></wbr>
  5. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$bootstrap</span><span><wbr></wbr></span>=<span><wbr></wbr></span><span style="color:rgb(0,0,255)">$this</span>-&gt;<span style="color:rgb(0,102,0)">getInvokeArg</span><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(255,0,0)">'bootstrap'</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr></wbr></wbr>
  6. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$view</span><span><wbr></wbr></span>=<span><wbr></wbr></span><span style="color:rgb(0,0,255)">$bootstrap</span>-&gt;<span style="color:rgb(0,102,0)">getResource</span><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(255,0,0)">'view'</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr></wbr></wbr>
  7. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(128,128,128)"><em>//...</em></span></wbr></wbr></wbr></wbr>
  8. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">}</span></wbr></wbr>
  9. }

Dependency Tracking<wbr></wbr>

In addition to executing resource methods and plugins, it'snecessary to ensure that these are executed once and once only;these are meant to bootstrap an application, and executing multipletimes can lead to resource overhead.

At the same time, some resources may depend on other resourcesbeing executed. To solve these two issues,<wbr></wbr>Zend_Application_Bootstrap_BootstrapAbstract<wbr></wbr>providesa simple, effective mechanism for dependency tracking.

As noted previously, all resources -- whether methods or plugins --are bootstrapped by callingbootstrap($resource),where<wbr></wbr>$resource<wbr></wbr>isthe name of a resource, an array of resources, or, left empty,indicates all resources should be run.

If a resource depends on another resource, it shouldcall<wbr></wbr>bootstrap()<wbr></wbr>withinits code to ensure that resource has been executed. Subsequentcalls to it will then be ignored.

In a resource method, such a call would look like this:

  1. class <wbr></wbr>Bootstrap <wbr></wbr> extends <wbr></wbr>Zend_Application_Bootstrap_Bootstrap
  2. {
  3. <wbr><wbr> protected<span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>_initRequest<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span></wbr></wbr>
  4. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">{</span></wbr></wbr>
  5. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(128,128,128)"><em>// Ensure thefront controller is initialized</em></span></wbr></wbr></wbr></wbr>
  6. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$this</span>-&gt;<span style="color:rgb(0,102,0)">bootstrap</span><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(255,0,0)">'FrontController'</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr></wbr></wbr>
  7. <wbr></wbr>
  8. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(128,128,128)"><em>// Retrieve thefront controller from the bootstrap registry</em></span></wbr></wbr></wbr></wbr>
  9. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$front</span><span><wbr></wbr></span>=<span><wbr></wbr></span><span style="color:rgb(0,0,255)">$this</span>-&gt;<span style="color:rgb(0,102,0)">getResource</span><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(255,0,0)">'FrontController'</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr></wbr></wbr>
  10. <wbr></wbr>
  11. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$request</span><span><wbr></wbr></span>=<span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>new</strong></span><span><wbr></wbr></span>Zend_Controller_Request_Http<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr></wbr></wbr>
  12. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$request</span>-&gt;<span style="color:rgb(0,102,0)">setBaseUrl</span><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(255,0,0)">'/foo'</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr></wbr></wbr>
  13. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$front</span>-&gt;<span style="color:rgb(0,102,0)">setRequest</span><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(0,0,255)">$request</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr></wbr></wbr>
  14. <wbr></wbr>
  15. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(128,128,128)"><em>// Ensure therequest is stored in the bootstrap registry</em></span></wbr></wbr></wbr></wbr>
  16. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(177,177,0)">return</span><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$request</span>;</wbr></wbr></wbr></wbr>
  17. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">}</span></wbr></wbr>
  18. }

Resource Plugins<wbr></wbr>

As noted previously, a good way to create re-usable bootstrapresources and to offload much of your coding to discrete classes isto utilize resource plugins. While Zend Framework ships with anumber of standard resource plugins, the intention is thatdevelopers should write their own to encapsulate their owninitialization needs.

Resource plugins need only implement<wbr></wbr>Zend_Application_Resource_Resource,or, more simply still, extend<wbr></wbr>Zend_Application_Resource_ResourceAbstract.The basic interface is simply this:

  1. interface <wbr></wbr>Zend_Application_Resource_Resource
  2. {
  3. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>__construct<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(0,0,255)">$options</span><span><wbr></wbr></span>=<span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>null</strong></span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  4. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>setBootstrap<span style="color:rgb(102,204,102)">(</span></wbr></wbr>
  5. <wbr><wbr><wbr><wbr>Zend_Application_Bootstrap_Bootstrapper<span><wbr></wbr></span><span style="color:rgb(0,0,255)">$bootstrap</span></wbr></wbr></wbr></wbr>
  6. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  7. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>getBootstrap<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  8. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>setOptions<span style="color:rgb(102,204,102)">(</span><a href="http://www.php.net/array"><span style="color:rgb(0,0,102)">array</span></a><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$options</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  9. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>getOptions<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  10. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>init<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr>
  11. }

The interface defines simply that a resource plugin should acceptoptions to the constructor, have mechanisms for setting andretrieving options, have mechanisms for setting and retrieving thebootstrap object, and an initialization method.

As an example, let's assume you have a common view intializationyou use in your applications. You have a commondoctype,<wbr></wbr>CSS<wbr></wbr>andJavaScript, and you want to be able to pass in a base documenttitle via configuration. Such a resource plugin might look likethis:

  1. class <wbr></wbr>My_Resource_View <wbr></wbr> extendsZend_Application_Resource_ResourceAbstract
  2. {
  3. <wbr><wbr> protected<span><wbr></wbr></span><span style="color:rgb(0,0,255)">$_view</span>;</wbr></wbr>
  4. <wbr></wbr>
  5. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>init<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span></wbr></wbr>
  6. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">{</span></wbr></wbr>
  7. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(128,128,128)"><em>// Return view sobootstrap will store it in the registry</em></span></wbr></wbr></wbr></wbr>
  8. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(177,177,0)">return</span><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$this</span>-&gt;<span style="color:rgb(0,102,0)">getView</span><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr></wbr></wbr>
  9. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">}</span></wbr></wbr>
  10. <wbr></wbr>
  11. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>public</strong></span><span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>function</strong></span><span><wbr></wbr></span>getView<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span></wbr></wbr>
  12. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">{</span></wbr></wbr>
  13. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(177,177,0)">if</span><span><wbr></wbr></span><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(0,0,0)"><strong>null</strong></span><span><wbr></wbr></span>===<span><wbr></wbr></span><span style="color:rgb(0,0,255)">$this</span>-&gt;_view<span style="color:rgb(102,204,102)">)</span><span><wbr></wbr></span><span style="color:rgb(102,204,102)">{</span></wbr></wbr></wbr></wbr>
  14. <wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$options</span><span><wbr></wbr></span>=<span><wbr></wbr></span><span style="color:rgb(0,0,255)">$this</span>-&gt;<span style="color:rgb(0,102,0)">getOptions</span><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr></wbr></wbr></wbr></wbr>
  15. <wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$title</span><wbr><wbr>=<span><wbr></wbr></span><span style="color:rgb(255,0,0)">''</span>;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  16. <wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(177,177,0)">if</span><span><wbr></wbr></span><span style="color:rgb(102,204,102)">(</span><a href="http://www.php.net/array_key_exists"><span style="color:rgb(0,0,102)">array_key_exists</span></a><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(255,0,0)">'title'</span>,<span><wbr></wbr></span><span style="color:rgb(0,0,255)">$options</span><span style="color:rgb(102,204,102)">)</span><span style="color:rgb(102,204,102)">)</span><span><wbr></wbr></span><span style="color:rgb(102,204,102)">{</span></wbr></wbr></wbr></wbr></wbr></wbr>
  17. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$title</span><span><wbr></wbr></span>=<span><wbr></wbr></span><span style="color:rgb(0,0,255)">$options</span><span style="color:rgb(102,204,102)">[</span><span style="color:rgb(255,0,0)">'title'</span><span style="color:rgb(102,204,102)">]</span>;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  18. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><a href="http://www.php.net/unset"><span style="color:rgb(0,0,102)">unset</span></a><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(0,0,255)">$options</span><span style="color:rgb(102,204,102)">[</span><span style="color:rgb(255,0,0)">'title'</span><span style="color:rgb(102,204,102)">]</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  19. <wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">}</span></wbr></wbr></wbr></wbr></wbr></wbr>
  20. <wbr></wbr>
  21. <wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$view</span><span><wbr></wbr></span>=<span><wbr></wbr></span><span style="color:rgb(0,0,0)"><strong>new</strong></span><span><wbr></wbr></span>Zend_View<span style="color:rgb(102,204,102)">(</span><span style="color:rgb(0,0,255)">$options</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr></wbr></wbr></wbr></wbr>
  22. <wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$view</span>-&gt;<span style="color:rgb(0,102,0)">doctype</span><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(255,0,0)">'XHTML1_STRICT'</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr></wbr></wbr></wbr></wbr>
  23. <wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$view</span>-&gt;<span style="color:rgb(0,102,0)">headTitle</span><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(0,0,255)">$title</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr></wbr></wbr></wbr></wbr>
  24. <wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$view</span>-&gt;<span style="color:rgb(0,102,0)">headLink</span><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span>-&gt;<span style="color:rgb(0,102,0)">appendStylesheet</span><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(255,0,0)">'/css/site.css'</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr></wbr></wbr></wbr></wbr>
  25. <wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$view</span>-&gt;<span style="color:rgb(0,102,0)">headScript</span><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(102,204,102)">)</span>-&gt;<span style="color:rgb(0,102,0)">appendfile</span><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(255,0,0)">'/js/analytics.js'</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr></wbr></wbr></wbr></wbr>
  26. <wbr></wbr>
  27. <wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$viewRenderer</span><span><wbr></wbr></span>=</wbr></wbr></wbr></wbr></wbr></wbr>
  28. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Zend_Controller_Action_HelperBroker::<span style="color:rgb(0,102,0)">getStaticHelper</span><span style="color:rgb(102,204,102)">(</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  29. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(255,0,0)">'ViewRenderer'</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  30. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  31. <wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$viewRenderer</span>-&gt;<span style="color:rgb(0,102,0)">setView</span><span style="color:rgb(102,204,102)">(</span><span style="color:rgb(0,0,255)">$view</span><span style="color:rgb(102,204,102)">)</span>;</wbr></wbr></wbr></wbr></wbr></wbr>
  32. <wbr></wbr>
  33. <wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$this</span>-&gt;_view=<span><wbr></wbr></span><span style="color:rgb(0,0,255)">$view</span>;</wbr></wbr></wbr></wbr></wbr></wbr>
  34. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">}</span></wbr></wbr></wbr></wbr>
  35. <wbr><wbr><wbr><wbr><span><wbr></wbr></span><span style="color:rgb(177,177,0)">return</span><span><wbr></wbr></span><span style="color:rgb(0,0,255)">$this</span>-&gt;_view;</wbr></wbr></wbr></wbr>
  36. <wbr><wbr><span><wbr></wbr></span><span style="color:rgb(102,204,102)">}</span></wbr></wbr>
  37. }

As long as you register the prefix path for this resource plugin,you can then use it in your application. Even better, because ituses the plugin loader, you are effectively overriding the shipped"View" resource plugin, ensuring that your own is used instead.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值