Zend_Controller-Modular.xml 7.8 KB

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