Zend_Application-Examples.xml 8.2 KB

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