Zend_Controller-Router-Route-Chain.xml 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect3 id="zend.controller.router.routes.chain">
  4. <title>Zend_Controller_Router_Route_Chain</title>
  5. <para>
  6. <classname>Zend_Controller_Router_Route_Chain</classname> is a route which allows
  7. to chain multiple routes together. This allows you to chain
  8. hostname-routes and path routes, or multiple path routes for example.
  9. Chaining can be done either programatically or within a configuration
  10. file.
  11. </para>
  12. <note>
  13. <title>Parameter Priority</title>
  14. <para>
  15. When chaining routes together, the parameters of the outer route
  16. have a higher priority than the parameters of the inner route. Thus
  17. if you define a controller in the outer and in the inner route,
  18. the controller of the outer route will be selected.
  19. </para>
  20. </note>
  21. <para>
  22. When chaining programatically, there are two ways to achieve this. The
  23. first one is to create a new
  24. <classname>Zend_Controller_Router_Route_Chain</classname> instance and then
  25. calling the <methodname>chain()</methodname> method multiple times with all routes
  26. which should be chained together. The other way is to take the first
  27. route, e.g. a hostname route, and calling the <methodname>chain()</methodname>
  28. method on it with the route which should be appended to it. This
  29. will not modify the hostname route, but return a new instance of
  30. <classname>Zend_Controller_Router_Route_Chain</classname>, which then has both
  31. routes chained together:
  32. </para>
  33. <programlisting language="php"><![CDATA[
  34. // Create two routes
  35. $hostnameRoute = new Zend_Controller_Router_Route_Hostname(...);
  36. $pathRoute = new Zend_Controller_Router_Route(...);
  37. // First way, chain them via the chain route
  38. $chainedRoute = new Zend_Controller_Router_Route_Chain();
  39. $chainedRoute->chain($hostnameRoute)
  40. ->chain($pathRoute);
  41. // Second way, chain them directly
  42. $chainedRoute = $hostnameRoute->chain($pathRoute);
  43. ]]></programlisting>
  44. <para>
  45. When chaining routes together, their separator is a slash
  46. by default. There may be cases when you want to have a different
  47. separator:
  48. </para>
  49. <programlisting language="php"><![CDATA[
  50. // Create two routes
  51. $firstRoute = new Zend_Controller_Router_Route('foo');
  52. $secondRoute = new Zend_Controller_Router_Route('bar');
  53. // Chain them together with a different separator
  54. $chainedRoute = $firstRoute->chain($secondRoute, '-');
  55. // Assemble the route: "foo-bar"
  56. echo $chainedRoute->assemble();
  57. ]]></programlisting>
  58. <sect4 id="zend.controller.router.routes.chain.config">
  59. <title>Chain Routes via Zend_Config</title>
  60. <para>
  61. To chain routes together in a config file, there are additional
  62. parameters for the configuration of those. The simpler approach is
  63. to use the <property>chains</property> parameters. This one is simply a list
  64. of routes, which will be chained with the parent route. Neither the
  65. parent- nor the child-route will be added directly to the router but
  66. only the resulting chained route. The name of the chained route in
  67. the router will be the parent route name and the child route name
  68. concatenated with a dash (-) by default. A simple config in <acronym>XML</acronym>
  69. would look like this:
  70. </para>
  71. <programlisting language="xml"><![CDATA[
  72. <routes>
  73. <www type="Zend_Controller_Router_Route_Hostname">
  74. <route>www.example.com</route>
  75. <chains>
  76. <language type="Zend_Controller_Router_Route">
  77. <route>:language</route>
  78. <reqs language="[a-z]{2}">
  79. <chains>
  80. <index type="Zend_Controller_Router_Route_Static">
  81. <route></route>
  82. <defaults module="default" controller="index"
  83. action="index" />
  84. </index>
  85. <imprint type="Zend_Controller_Router_Route_Static">
  86. <route>imprint</route>
  87. <defaults module="default" controller="index"
  88. action="index" />
  89. </imprint>
  90. </chains>
  91. </language>
  92. </chains>
  93. </www>
  94. <users type="Zend_Controller_Router_Route_Hostname">
  95. <route>users.example.com</route>
  96. <chains>
  97. <profile type="Zend_Controller_Router_Route">
  98. <route>:username</route>
  99. <defaults module="users" controller="profile" action="index" />
  100. </profile>
  101. </chains>
  102. </users>
  103. <misc type="Zend_Controller_Router_Route_Static">
  104. <route>misc</route>
  105. </misc>
  106. </routes>
  107. ]]></programlisting>
  108. <para>
  109. This will result in the three routes <command>www-language-index</command>,
  110. <command>www-language-imprint</command> and
  111. <command>users-language-profile</command> which will only match based on
  112. the hostname and the route <command>misc</command>, which will match with
  113. any hostname.
  114. </para>
  115. <para>
  116. The alternative way of creating a chained route is via the
  117. <property>chain</property> parameter, which can only be used with the
  118. chain-route type directly, and also just works in the root level:
  119. </para>
  120. <programlisting language="xml"><![CDATA[
  121. <routes>
  122. <www type="Zend_Controller_Router_Route_Chain">
  123. <route>www.example.com</route>
  124. </www>
  125. <language type="Zend_Controller_Router_Route">
  126. <route>:language</route>
  127. <reqs language="[a-z]{2}">
  128. </language>
  129. <index type="Zend_Controller_Router_Route_Static">
  130. <route></route>
  131. <defaults module="default" controller="index" action="index" />
  132. </index>
  133. <imprint type="Zend_Controller_Router_Route_Static">
  134. <route>imprint</route>
  135. <defaults module="default" controller="index" action="index" />
  136. </imprint>
  137. <www-index type="Zend_Controller_Router_Route_Chain">
  138. <chain>www, language, index</chain>
  139. </www-index>
  140. <www-imprint type="Zend_Controller_Router_Route_Chain">
  141. <chain>www, language, imprint</chain>
  142. </www-imprint>
  143. </routes>
  144. ]]></programlisting>
  145. <para>
  146. You can also give the <property>chain</property> parameter as array instead
  147. of separating the routes with a comma:
  148. </para>
  149. <programlisting language="xml"><![CDATA[
  150. <routes>
  151. <www-index type="Zend_Controller_Router_Route_Chain">
  152. <chain>www</chain>
  153. <chain>language</chain>
  154. <chain>index</chain>
  155. </www-index>
  156. <www-imprint type="Zend_Controller_Router_Route_Chain">
  157. <chain>www</chain>
  158. <chain>language</chain>
  159. <chain>imprint</chain>
  160. </www-imprint>
  161. </routes>
  162. ]]></programlisting>
  163. <para>
  164. When you configure chain routes with <classname>Zend_Config</classname> and
  165. want the chain name separator to be different from a dash, you
  166. need to specify this separator separately:
  167. </para>
  168. <programlisting language="php"><![CDATA[
  169. $config = new Zend_Config(array(
  170. 'chainName' => array(
  171. 'type' => 'Zend_Controller_Router_Route_Static',
  172. 'route' => 'foo',
  173. 'chains' => array(
  174. 'subRouteName' => array(
  175. 'type' => 'Zend_Controller_Router_Route_Static',
  176. 'route' => 'bar',
  177. 'defaults' => array(
  178. 'module' => 'module',
  179. 'controller' => 'controller',
  180. 'action' => 'action'
  181. )
  182. )
  183. )
  184. )
  185. ));
  186. // Set separator before adding config
  187. $router->setChainNameSeparator('_separator_')
  188. // Add config
  189. $router->addConfig($config);
  190. // The name of our route now is: chainName_separator_subRouteName
  191. echo $this->_router->assemble(array(), 'chainName_separator_subRouteName');
  192. // The proof: it echoes /foo/bar
  193. ]]></programlisting>
  194. </sect4>
  195. </sect3>
  196. <!--
  197. vim:se ts=4 sw=4 et:
  198. -->