Zend_Application-Examples.xml 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. [testing : production]
  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
  95. <classname>Zend_Application_Resource_ResourceAbstract</classname>,
  96. which defines simply that they allow injection of the caller and
  97. options, and that they have an <methodname>init()</methodname> method. As an
  98. example, a custom "View" bootstrap resource might look like the
  99. following:
  100. </para>
  101. <programlisting language="php"><![CDATA[
  102. class My_Bootstrap_Resource_View
  103. extends Zend_Application_Resource_ResourceAbstract
  104. {
  105. public function init()
  106. {
  107. $view = new Zend_View($this->getOptions());
  108. Zend_Dojo::enableView($view);
  109. $view->doctype('XHTML1_STRICT');
  110. $view->headTitle()->setSeparator(' - ')->append('My Site');
  111. $view->headMeta()->appendHttpEquiv('Content-Type',
  112. 'text/html; charset=utf-8');
  113. $view->dojo()->setDjConfigOption('parseOnLoad', true)
  114. ->setLocalPath('/js/dojo/dojo.js')
  115. ->registerModulePath('../spindle', 'spindle')
  116. ->addStylesheetModule('spindle.themes.spindle')
  117. ->requireModule('spindle.main')
  118. ->disable();
  119. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
  120. 'ViewRenderer'
  121. );
  122. $viewRenderer->setView($view);
  123. return $view;
  124. }
  125. }
  126. ]]></programlisting>
  127. <para>
  128. To tell the bootstrap to use this, you would need to provide either the
  129. class name of the resource plugin, or a combination of a plugin loader prefix
  130. path and the short name of the resource plugin (e.g, "view"):
  131. </para>
  132. <programlisting language="php"><![CDATA[
  133. $application = new Zend_Application(
  134. APPLICATION_ENV,
  135. array(
  136. 'resources' => array(
  137. 'My_Bootstrap_Resource_View' => array(), // full class name; OR
  138. 'view' => array(), // short name
  139. 'FrontController' => array(
  140. 'controllerDirectory' => APPLICATION_PATH . '/controllers',
  141. ),
  142. ),
  143. // For short names, define plugin paths:
  144. 'pluginPaths = array(
  145. 'My_Bootstrap_Resource' => 'My/Bootstrap/Resource',
  146. )
  147. )
  148. );
  149. ]]></programlisting>
  150. <para>
  151. Resource plugins can call on other resources and initializers by accessing the
  152. parent bootstrap:
  153. </para>
  154. <programlisting language="php"><![CDATA[
  155. class My_Bootstrap_Resource_Layout
  156. extends Zend_Application_Resource_ResourceAbstract
  157. {
  158. public function init()
  159. {
  160. // ensure view is initialized...
  161. $this->getBootstrap()->bootstrap('view');
  162. // Get view object:
  163. $view = $this->getBootstrap()->getResource('view');
  164. // ...
  165. }
  166. }
  167. ]]></programlisting>
  168. <para>
  169. In normal usage, you would instantiate the application, bootstrap it,
  170. and run it:
  171. </para>
  172. <programlisting language="php"><![CDATA[
  173. $application = new Zend_Application(...);
  174. $application->bootstrap()
  175. ->run();
  176. ]]></programlisting>
  177. <para>
  178. For a custom script, you might need to simply initialize specific
  179. resources:
  180. </para>
  181. <programlisting language="php"><![CDATA[
  182. $application = new Zend_Application(...);
  183. $application->getBootstrap()->bootstrap('db');
  184. $service = new Zend_XmlRpc_Server();
  185. $service->setClass('Foo'); // uses database...
  186. echo $service->handle();
  187. ]]></programlisting>
  188. <para>
  189. Instead of using the <methodname>bootstrap()</methodname> method to call the
  190. internal methods or resources, you may also use overloading:
  191. </para>
  192. <programlisting language="php"><![CDATA[
  193. $application = new Zend_Application(...);
  194. $application->getBootstrap()->bootstrapDb();
  195. ]]></programlisting>
  196. </sect1>