Documentation

Examples - Zend_Application

Examples

The Bootstrap class itself will typically be fairly minimal; often, it will simply be an empty stub extending the base bootstrap class:

  1. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  2. {
  3. }

With a corresponding configuration file:

  1. ; APPLICATION_PATH/configs/application.ini
  2. [production]
  3. autoloaderNamespaces[] = "My_"
  4. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  5. bootstrap.class = "Bootstrap"
  6. pluginpaths.My_Bootstrap_Resource = "My/Bootstrap/Resource"
  7. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  8.  
  9. [testing : production]
  10. [development : production]

Note: Autoloader namespaces
Because these examples use custom code, we need to register the namespace prefixes for that code with our configuration; this is done with the autoloaderNamespaces configuration key, which is an array.
Additionally, to ensure that custom plugin resources are discovered, we need to register a plugin prefix path with the bootstrap. This is done with the pluginpaths configuration key, which is an associative array, with keys denoting the prefix to use, and values denoting the path related to that prefix.

However, should custom initialization be necessary, you have two choices. First, you can write methods prefixed with _init to specify discrete code to bootstrap. These methods will be called by bootstrap(), and can also be called as if they were public methods: bootstrap<resource>(). They should accept an optional array of options.

If your resource method returns a value, it will be stored in a container in the bootstrap. This can be useful when different resources need to interact (such as one resource injecting itself into another). The method getResource() can then be used to retrieve those values.

The example below shows a resource method for initializing the request object. It makes use of dependency tracking (it depends on the front controller resource), fetching a resource from the bootstrap, and returning a value to store in the bootstrap.

  1. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  2. {
  3.     protected function _initRequest()
  4.     {
  5.         // Ensure front controller instance is present, and fetch it
  6.         $this->bootstrap('FrontController');
  7.         $front = $this->getResource('FrontController');
  8.  
  9.         // Initialize the request object
  10.         $request = new Zend_Controller_Request_Http();
  11.         $request->setBaseUrl('/foo');
  12.  
  13.         // Add it to the front controller
  14.         $front->setRequest($request);
  15.  
  16.         // Bootstrap will store this value in the 'request' key of its container
  17.         return $request;
  18.     }
  19. }

Note in this example the call to bootstrap(); this ensures that the front controller has been initialized prior to calling this method. That call may trigger either a resource or another method in the class.

The other option is to use resource plugins. Resource plugins are objects that perform specific initializations, and may be specified:

  • When instantiating the Zend_Application object

  • During initialization of the bootstrap object

  • By explicitly enabling them via method calls to the bootstrap object

Resource plugins implement Zend_Application_Resource_ResourceAbstract, which defines simply that they allow injection of the caller and options, and that they have an init() method. As an example, a custom "View" bootstrap resource might look like the following:

  1. class My_Bootstrap_Resource_View
  2.     extends Zend_Application_Resource_ResourceAbstract
  3. {
  4.     public function init()
  5.     {
  6.         $view = new Zend_View($this->getOptions());
  7.         Zend_Dojo::enableView($view);
  8.  
  9.         $view->doctype('XHTML1_STRICT');
  10.         $view->headTitle()->setSeparator(' - ')->append('My Site');
  11.         $view->headMeta()->appendHttpEquiv('Content-Type',
  12.                                            'text/html; charset=utf-8');
  13.  
  14.         $view->dojo()->setDjConfigOption('parseOnLoad', true)
  15.                      ->setLocalPath('/js/dojo/dojo.js')
  16.                      ->registerModulePath('../spindle', 'spindle')
  17.                      ->addStylesheetModule('spindle.themes.spindle')
  18.                      ->requireModule('spindle.main')
  19.                      ->disable();
  20.  
  21.         $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
  22.             'ViewRenderer'
  23.         );
  24.         $viewRenderer->setView($view);
  25.  
  26.         return $view;
  27.     }
  28. }

To tell the bootstrap to use this, you would need to provide either the class name of the resource plugin, or a combination of a plugin loader prefix path and the short name of the resource plugin (e.g, "view"):

  1. $application = new Zend_Application(
  2.     APPLICATION_ENV,
  3.     array(
  4.         'resources' => array(
  5.             'My_Bootstrap_Resource_View' => array(), // full class name; OR
  6.             'view' => array(),                       // short name
  7.  
  8.             'FrontController' => array(
  9.                 'controllerDirectory' => APPLICATION_PATH . '/controllers',
  10.             ),
  11.         ),
  12.  
  13.         // For short names, define plugin paths:
  14.         'pluginPaths = array(
  15.             'My_Bootstrap_Resource' => 'My/Bootstrap/Resource',
  16.         )
  17.     )
  18. );

Resource plugins can call on other resources and initializers by accessing the parent bootstrap:

  1. class My_Bootstrap_Resource_Layout
  2.     extends Zend_Application_Resource_ResourceAbstract
  3. {
  4.     public function init()
  5.     {
  6.         // ensure view is initialized...
  7.         $this->getBootstrap()->bootstrap('view');
  8.  
  9.         // Get view object:
  10.         $view = $this->getBootstrap()->getResource('view');
  11.  
  12.         // ...
  13.     }
  14. }

In normal usage, you would instantiate the application, bootstrap it, and run it:

  1. $application = new Zend_Application(...);
  2. $application->bootstrap()
  3.             ->run();

For a custom script, you might need to simply initialize specific resources:

  1. $application = new Zend_Application(...);
  2. $application->getBootstrap()->bootstrap('db');
  3.  
  4. $service = new Zend_XmlRpc_Server();
  5. $service->setClass('Foo')// uses database...
  6. echo $service->handle();

Instead of using the bootstrap() method to call the internal methods or resources, you may also use overloading:

  1. $application = new Zend_Application(...);
  2. $application->getBootstrap()->bootstrapDb();

Copyright

© 2006-2021 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts