Zend_Controller-Modular.xml 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.controller.modular">
  4. <title>Using a Conventional Modular Directory Structure</title>
  5. <sect2 id="zend.controller.modular.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. The Conventional Modular directory structure allows you to separate
  9. different <acronym>MVC</acronym> applications into self-contained units, and re-use
  10. them with different front controllers. To illustrate such a
  11. directory structure:
  12. </para>
  13. <programlisting language="txt"><![CDATA[
  14. docroot/
  15. index.php
  16. application/
  17. default/
  18. controllers/
  19. IndexController.php
  20. FooController.php
  21. models/
  22. views/
  23. scripts/
  24. index/
  25. foo/
  26. helpers/
  27. filters/
  28. blog/
  29. controllers/
  30. IndexController.php
  31. models/
  32. views/
  33. scripts/
  34. index/
  35. helpers/
  36. filters/
  37. news/
  38. controllers/
  39. IndexController.php
  40. ListController.php
  41. models/
  42. views/
  43. scripts/
  44. index/
  45. list/
  46. helpers/
  47. filters/
  48. ]]></programlisting>
  49. <para>
  50. In this paradigm, the module name serves as a prefix to the
  51. controllers it contains. The above example contains three
  52. module controllers, '<classname>Blog_IndexController</classname>',
  53. '<classname>News_IndexController</classname>', and
  54. '<classname>News_ListController</classname>'.
  55. Two global controllers, '<classname>IndexController</classname>' and
  56. '<classname>FooController</classname>' are also defined; neither of these will be
  57. namespaced. This directory structure will be used for examples in
  58. this chapter.
  59. </para>
  60. <note>
  61. <title>No Namespacing in the Default Module</title>
  62. <para>
  63. Note that in the default module, controllers do not need a
  64. namespace prefix. Thus, in the example above, the controllers in
  65. the default module do not need a prefix of 'Default_' -- they
  66. are simply dispatched according to their base controller name:
  67. '<classname>IndexController</classname>' and
  68. '<classname>FooController</classname>'. A namespace prefix is
  69. used in all other modules, however.
  70. </para>
  71. </note>
  72. <para>
  73. So, how do you implement such a directory layout using the Zend
  74. Framework <acronym>MVC</acronym> components?
  75. </para>
  76. </sect2>
  77. <sect2 id="zend.controller.modular.directories">
  78. <title>Specifying Module Controller Directories</title>
  79. <para>
  80. The first step to making use of modules is to modify how you specify
  81. the controller directory list in the front controller. In the basic
  82. <acronym>MVC</acronym> series, you pass either an array or a string to
  83. <methodname>setControllerDirectory()</methodname>, or a path to
  84. <methodname>addControllerDirectory()</methodname>. When using modules, you need
  85. to alter your calls to these methods slightly.
  86. </para>
  87. <para>
  88. With <methodname>setControllerDirectory()</methodname>, you will need to pass an
  89. associative array and specify key and value pairs of module
  90. name and directory paths. The special key <property>default</property> will be
  91. used for global controllers (those not needing a module namespace).
  92. All entries should contain a string key pointing to a single path,
  93. and the <property>default</property> key must be present. As an example:
  94. </para>
  95. <programlisting language="php"><![CDATA[
  96. $front->setControllerDirectory(array(
  97. 'default' => '/path/to/application/controllers',
  98. 'blog' => '/path/to/application/blog/controllers'
  99. ));
  100. ]]></programlisting>
  101. <para>
  102. <methodname>addControllerDirectory()</methodname> will take an optional second
  103. argument. When using modules, pass the module name as the second
  104. argument; if not specified, the path will be added to the
  105. <emphasis>default</emphasis> namespace. As an example:
  106. </para>
  107. <programlisting language="php"><![CDATA[
  108. $front->addControllerDirectory('/path/to/application/news/controllers',
  109. 'news');
  110. ]]></programlisting>
  111. <para>
  112. Saving the best for last, the easiest way to specify module
  113. directories is to do so en masse, with all modules under a common
  114. directory and sharing the same structure. This can be done with
  115. <methodname>addModuleDirectory()</methodname>:
  116. </para>
  117. <programlisting language="php"><![CDATA[
  118. /**
  119. * Assuming the following directory structure:
  120. * application/
  121. * modules/
  122. * default/
  123. * controllers/
  124. * foo/
  125. * controllers/
  126. * bar/
  127. * controllers/
  128. */
  129. $front->addModuleDirectory('/path/to/application/modules');
  130. ]]></programlisting>
  131. <para>
  132. The above example will define the <emphasis>default</emphasis>,
  133. <emphasis>foo</emphasis>, and <emphasis>bar</emphasis> modules, each pointing to the
  134. <filename>controllers/</filename> subdirectory of their respective module.
  135. </para>
  136. <para>
  137. You may customize the controller subdirectory to use within your
  138. modules by using <methodname>setModuleControllerDirectoryName()</methodname>:
  139. </para>
  140. <programlisting language="php"><![CDATA[
  141. /**
  142. * Change the controllers subdirectory to be 'con'
  143. * application/
  144. * modules/
  145. * default/
  146. * con/
  147. * foo/
  148. * con/
  149. * bar/
  150. * con/
  151. */
  152. $front->setModuleControllerDirectoryName('con');
  153. $front->addModuleDirectory('/path/to/application/modules');
  154. ]]></programlisting>
  155. <note>
  156. <para>
  157. You can indicate that no controller subdirectory be used for your
  158. modules by passing an empty value to
  159. <methodname>setModuleControllerDirectoryName()</methodname>.
  160. </para>
  161. </note>
  162. </sect2>
  163. <sect2 id="zend.controller.modular.router">
  164. <title>Routing to Modules</title>
  165. <para>
  166. The default route in <classname>Zend_Controller_Router_Rewrite</classname> is
  167. an object of type <classname>Zend_Controller_Router_Route_Module</classname>.
  168. This route expects one of the following routing schemas:
  169. </para>
  170. <itemizedlist>
  171. <listitem><para><filename>:module/:controller/:action/*</filename></para></listitem>
  172. <listitem><para><filename>:controller/:action/*</filename></para></listitem>
  173. </itemizedlist>
  174. <para>
  175. In other words, it will match a controller and action by themselves
  176. or with a preceding module. The rules for matching specify that a
  177. module will only be matched if a key of the same name exists in the
  178. controller directory array passed to the front controller and
  179. dispatcher.
  180. </para>
  181. </sect2>
  182. <sect2 id="zend.controller.modular.defaultcontroller">
  183. <title>Module or Global Default Controller</title>
  184. <para>
  185. In the default router, if a controller was not specified in the <acronym>URL</acronym>,
  186. a default controller is used (<classname>IndexController</classname>, unless
  187. otherwise requested). With modular controllers, if a module has been
  188. specified but no controller, the dispatcher first looks for this
  189. default controller in the module path, and then falls back on the
  190. default controller found in the 'default', global, namespace.
  191. </para>
  192. <para>
  193. If you wish to always default to the global namespace, set the
  194. <varname>$useDefaultControllerAlways</varname> parameter in the front controller:
  195. </para>
  196. <programlisting language="php"><![CDATA[
  197. $front->setParam('useDefaultControllerAlways', true);
  198. ]]></programlisting>
  199. </sect2>
  200. </sect1>
  201. <!--
  202. vim:se ts=4 sw=4 et:
  203. -->