2
0

Zend_Controller-Modular.xml 7.9 KB

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