Zend_Application-Examples.xml 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.application.examples">
  4. <title>Examples</title>
  5. <para>
  6. The Bootstrap class itself will typically be fairly minimal; often,
  7. it will simply be an empty stub extending the base bootstrap class:
  8. </para>
  9. <programlisting language="php"><![CDATA[
  10. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  11. {
  12. }
  13. ]]></programlisting>
  14. <para>
  15. With a corresponding configuration file:
  16. </para>
  17. <programlisting language="ini"><![CDATA[
  18. ; APPLICATION_PATH/configs/application.ini
  19. [production]
  20. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  21. bootstrap.class = "Bootstrap"
  22. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  23. [development : testing]
  24. [development : production]
  25. ]]></programlisting>
  26. <para>
  27. However, should custom initialization be necessary, you have two
  28. choices. First, you can write methods prefixed with <emphasis>_init</emphasis>
  29. to specify discrete code to bootstrap. These methods will be called by
  30. <methodname>bootstrap()</methodname>, and can also be called as if they were public methods:
  31. <emphasis>bootstrap&lt;resource&gt;()</emphasis>. They should accept an optional
  32. array of options.
  33. </para>
  34. <para>
  35. If your resource method returns a value, it will be stored in a
  36. container in the bootstrap. This can be useful when different resources
  37. need to interact (such as one resource injecting itself into another).
  38. The method <methodname>getResource()</methodname> can then be used to retrieve those
  39. values.
  40. </para>
  41. <para>
  42. The example below shows a resource method for initializing the request
  43. object. It makes use of dependency tracking (it depends on the front
  44. controller resource), fetching a resource from the bootstrap, and
  45. returning a value to store in the bootstrap.
  46. </para>
  47. <programlisting language="php"><![CDATA[
  48. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  49. {
  50. protected function _initRequest(array $options = array())
  51. {
  52. // Ensure front controller instance is present, and fetch it
  53. $this->bootstrap('FrontController');
  54. $front = $this->getResource('FrontController');
  55. // Initialize the request object
  56. $request = new Zend_Controller_Request_Http();
  57. $request->setBaseUrl('/foo');
  58. // Add it to the front controller
  59. $front->setRequest($request);
  60. // Bootstrap will store this value in the 'request' key of its container
  61. return $request;
  62. }
  63. }
  64. ]]></programlisting>
  65. <para>
  66. Note in this example the call to <methodname>bootstrap()</methodname>;
  67. this ensures that the front controller has been initialized prior to
  68. calling this method. That call may trigger either a resource or another
  69. method in the class.
  70. </para>
  71. <para>
  72. The other option is to use resource plugins. Resource plugins are
  73. objects that perform specific initializations, and may be specified:
  74. </para>
  75. <itemizedlist>
  76. <listitem>
  77. <para>
  78. When instantiating the <classname>Zend_Application</classname> object
  79. </para>
  80. </listitem>
  81. <listitem>
  82. <para>
  83. During initialization of the bootstrap object
  84. </para>
  85. </listitem>
  86. <listitem>
  87. <para>
  88. By explicitly enabling them via method calls to the bootstrap
  89. object
  90. </para>
  91. </listitem>
  92. </itemizedlist>
  93. <para>
  94. Resource plugins implement <classname>Zend_Application_Bootstrap_Resource</classname>,
  95. which defines simply that they allow injection of the caller and
  96. options, and that they have an <methodname>init()</methodname> method. As an
  97. example, a custom "View" bootstrap resource might look like the
  98. following:
  99. </para>
  100. <programlisting language="php"><![CDATA[
  101. class My_Bootstrap_Resource_View
  102. extends Zend_Application_Resource_ResourceAbstract
  103. {
  104. public function init()
  105. {
  106. $view = new Zend_View($this->getOptions());
  107. Zend_Dojo::enableView($view);
  108. $view->doctype('XHTML1_STRICT');
  109. $view->headTitle()->setSeparator(' - ')->append('My Site');
  110. $view->headMeta()->appendHttpEquiv('Content-Type',
  111. 'text/html; charset=utf-8');
  112. $view->dojo()->setDjConfigOption('parseOnLoad', true)
  113. ->setLocalPath('/js/dojo/dojo.js')
  114. ->registerModulePath('../spindle', 'spindle')
  115. ->addStylesheetModule('spindle.themes.spindle')
  116. ->requireModule('spindle.main')
  117. ->disable();
  118. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
  119. 'ViewRenderer'
  120. );
  121. $viewRenderer->setView($view);
  122. return $view;
  123. }
  124. }
  125. ]]></programlisting>
  126. <para>
  127. To tell the bootstrap to use this, you would need to provide either the
  128. class name of the resource, or a combination of a plugin loader prefix
  129. path and the short name of the resource (e.g, "view"):
  130. </para>
  131. <programlisting language="php"><![CDATA[
  132. $application = new Zend_Application(
  133. APPLICATION_ENV,
  134. array(
  135. 'resources' => array(
  136. 'My_Bootstrap_Resource_View' => array(), // full class name; OR
  137. 'view' => array(), // short name
  138. 'FrontController' => array(
  139. 'controllerDirectory' => APPLICATION_PATH . '/controllers',
  140. ),
  141. ),
  142. // For short names, define plugin paths:
  143. 'pluginPaths = array(
  144. 'My_Bootstrap_Resource' => 'My/Bootstrap/Resource',
  145. )
  146. )
  147. );
  148. ]]></programlisting>
  149. <para>
  150. Resources can call on other resources and initializers by accessing the
  151. parent bootstrap:
  152. </para>
  153. <programlisting language="php"><![CDATA[
  154. class My_Bootstrap_Resource_Layout
  155. extends Zend_Application_Resource_ResourceAbstract
  156. {
  157. public function init()
  158. {
  159. // ensure view is initialized...
  160. $this->getBootstrap()->bootstrap('view');
  161. // Get view object:
  162. $view = $this->getBootstrap()->getResource('view');
  163. // ...
  164. }
  165. }
  166. ]]></programlisting>
  167. <para>
  168. In normal usage, you would instantiate the application, bootstrap it,
  169. and run it:
  170. </para>
  171. <programlisting language="php"><![CDATA[
  172. $application = new Zend_Application(...);
  173. $application->bootstrap()
  174. ->run();
  175. ]]></programlisting>
  176. <para>
  177. For a custom script, you might need to simply initialize specific
  178. resources:
  179. </para>
  180. <programlisting language="php"><![CDATA[
  181. $application = new Zend_Application(...);
  182. $application->getBootstrap()->bootstrap('db');
  183. $service = new Zend_XmlRpc_Server();
  184. $service->setClass('Foo'); // uses database...
  185. echo $service->handle();
  186. ]]></programlisting>
  187. <para>
  188. Instead of using the <methodname>bootstrap()</methodname> method to call the
  189. internal methods or resources, you may also use overloading:
  190. </para>
  191. <programlisting language="php"><![CDATA[
  192. $application = new Zend_Application(...);
  193. $application->getBootstrap()->bootstrapDb();
  194. ]]></programlisting>
  195. </sect1>