1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.application.examples">
4 <title>Examples</title>
7 The Bootstrap class itself will typically be fairly minimal; often,
8 it will simply be an empty stub extending the base bootstrap class:
11 <programlisting language="php"><![CDATA[
12 class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
18 With a corresponding configuration file:
21 <programlisting language="ini"><![CDATA[
22 ; APPLICATION_PATH/configs/application.ini
24 bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
25 bootstrap.class = "Bootstrap"
26 resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
28 [testing : production]
29 [development : production]
33 However, should custom initialization be necessary, you have two
34 choices. First, you can write methods prefixed with <emphasis>_init</emphasis>
35 to specify discrete code to bootstrap. These methods will be called by
36 <methodname>bootstrap()</methodname>, and can also be called as if they were public methods:
37 <emphasis>bootstrap<resource>()</emphasis>. They should accept an optional
42 If your resource method returns a value, it will be stored in a
43 container in the bootstrap. This can be useful when different resources
44 need to interact (such as one resource injecting itself into another).
45 The method <methodname>getResource()</methodname> can then be used to retrieve those
50 The example below shows a resource method for initializing the request
51 object. It makes use of dependency tracking (it depends on the front
52 controller resource), fetching a resource from the bootstrap, and
53 returning a value to store in the bootstrap.
56 <programlisting language="php"><![CDATA[
57 class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
59 protected function _initRequest()
61 // Ensure front controller instance is present, and fetch it
62 $this->bootstrap('FrontController');
63 $front = $this->getResource('FrontController');
65 // Initialize the request object
66 $request = new Zend_Controller_Request_Http();
67 $request->setBaseUrl('/foo');
69 // Add it to the front controller
70 $front->setRequest($request);
72 // Bootstrap will store this value in the 'request' key of its container
79 Note in this example the call to <methodname>bootstrap()</methodname>;
80 this ensures that the front controller has been initialized prior to
81 calling this method. That call may trigger either a resource or another
86 The other option is to use resource plugins. Resource plugins are
87 objects that perform specific initializations, and may be specified:
93 When instantiating the <classname>Zend_Application</classname> object
99 During initialization of the bootstrap object
105 By explicitly enabling them via method calls to the bootstrap
112 Resource plugins implement
113 <classname>Zend_Application_Resource_ResourceAbstract</classname>,
114 which defines simply that they allow injection of the caller and
115 options, and that they have an <methodname>init()</methodname> method. As an
116 example, a custom "View" bootstrap resource might look like the
120 <programlisting language="php"><![CDATA[
121 class My_Bootstrap_Resource_View
122 extends Zend_Application_Resource_ResourceAbstract
124 public function init()
126 $view = new Zend_View($this->getOptions());
127 Zend_Dojo::enableView($view);
129 $view->doctype('XHTML1_STRICT');
130 $view->headTitle()->setSeparator(' - ')->append('My Site');
131 $view->headMeta()->appendHttpEquiv('Content-Type',
132 'text/html; charset=utf-8');
134 $view->dojo()->setDjConfigOption('parseOnLoad', true)
135 ->setLocalPath('/js/dojo/dojo.js')
136 ->registerModulePath('../spindle', 'spindle')
137 ->addStylesheetModule('spindle.themes.spindle')
138 ->requireModule('spindle.main')
141 $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
144 $viewRenderer->setView($view);
152 To tell the bootstrap to use this, you would need to provide either the
153 class name of the resource plugin, or a combination of a plugin loader prefix
154 path and the short name of the resource plugin (e.g, "view"):
157 <programlisting language="php"><![CDATA[
158 $application = new Zend_Application(
161 'resources' => array(
162 'My_Bootstrap_Resource_View' => array(), // full class name; OR
163 'view' => array(), // short name
165 'FrontController' => array(
166 'controllerDirectory' => APPLICATION_PATH . '/controllers',
170 // For short names, define plugin paths:
171 'pluginPaths = array(
172 'My_Bootstrap_Resource' => 'My/Bootstrap/Resource',
179 Resource plugins can call on other resources and initializers by accessing the
183 <programlisting language="php"><![CDATA[
184 class My_Bootstrap_Resource_Layout
185 extends Zend_Application_Resource_ResourceAbstract
187 public function init()
189 // ensure view is initialized...
190 $this->getBootstrap()->bootstrap('view');
193 $view = $this->getBootstrap()->getResource('view');
201 In normal usage, you would instantiate the application, bootstrap it,
205 <programlisting language="php"><![CDATA[
206 $application = new Zend_Application(...);
207 $application->bootstrap()
212 For a custom script, you might need to simply initialize specific
216 <programlisting language="php"><![CDATA[
217 $application = new Zend_Application(...);
218 $application->getBootstrap()->bootstrap('db');
220 $service = new Zend_XmlRpc_Server();
221 $service->setClass('Foo'); // uses database...
222 echo $service->handle();
226 Instead of using the <methodname>bootstrap()</methodname> method to call the
227 internal methods or resources, you may also use overloading:
230 <programlisting language="php"><![CDATA[
231 $application = new Zend_Application(...);
232 $application->getBootstrap()->bootstrapDb();