Zend_Controller-Router-Route-Chain.xml 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 24249 -->
  4. <sect3 id="zend.controller.router.routes.chain">
  5. <title>Zend_Controller_Router_Route_Chain(日本語)</title>
  6. <para>
  7. <classname>Zend_Controller_Router_Route_Chain</classname> は、
  8. 複数のルートを一緒にチェーンできるルートです。
  9. これは、たとえばホスト名とルート、パスとルート、または複数のパスとルートをチェーンできます。
  10. チェーンは、プログラム的に、または、構成ファイルの範囲内で行なえます。
  11. </para>
  12. <note>
  13. <title>パラメータ優先度</title>
  14. <para>
  15. ルートを一緒にチェーンするとき、
  16. 外側のルートのパラメータは内側のルートのパラメータより高い優先度を持ちます。
  17. そういうわけで、外側のもので、そして、内側のルートでコントローラを定義するなら、
  18. 外側のルートのコントローラが選ばれます。
  19. </para>
  20. </note>
  21. <para>
  22. プログラム的にチェーンするとき、これを達成する2つの方法があります。
  23. 最初の1つは、 <classname>Zend_Controller_Router_Route_Chain</classname>
  24. インスタンスを新規作成して、
  25. そして、一緒にチェーンでつながなければならないルートすべてで
  26. <methodname>chain()</methodname> メソッドを複数回呼ぶことです。
  27. 他の方法は、最初のルート(例えばホスト名のルート)を受け取って、
  28. それに付加されなければならないルートとともに、
  29. そのルート上で <methodname>chain()</methodname> メソッドを呼ぶことです。
  30. これはホスト名ルートを修正せずとも、
  31. <classname>Zend_Controller_Router_Route_Chain</classname> の新規インスタンスを返します。
  32. そして、両方のルートは一緒につながれます。
  33. </para>
  34. <programlisting language="php"><![CDATA[
  35. //ルートを2つ作成
  36. $hostnameRoute = new Zend_Controller_Router_Route_Hostname(...);
  37. $pathRoute = new Zend_Controller_Router_Route(...);
  38. //最初の方法では、チェーン・ルートを通じてそれらをチェーンします。
  39. $chainedRoute = new Zend_Controller_Router_Route_Chain();
  40. $chainedRoute->chain($hostnameRoute)
  41. ->chain($pathRoute);
  42. //次の方法では、それらを直接チェーンします。
  43. $chainedRoute = $hostnameRoute->chain($pathRoute);
  44. ]]></programlisting>
  45. <para>
  46. ルートを一緒にチェーンするとき、それらの分離記号はデフォルトでスラッシュです。
  47. 異なる分離記号にしたい場合があるかもしれません。
  48. </para>
  49. <programlisting language="php"><![CDATA[
  50. //ルートを2つ作成
  51. $firstRoute = new Zend_Controller_Router_Route('foo');
  52. $secondRoute = new Zend_Controller_Router_Route('bar');
  53. //それらを異なる分離記号で一緒にチェーンします。
  54. $chainedRoute = $firstRoute->chain($secondRoute, '-');
  55. //ルートを結合します: "foo-bar"
  56. echo $chainedRoute->assemble();
  57. ]]></programlisting>
  58. <sect4 id="zend.controller.router.routes.chain.config">
  59. <title>Zend_Configを介したルートのチェーン</title>
  60. <para>
  61. 構成ファイルでルートをチェーンするために、それらの構成のための付加パラメータがあります。
  62. より単純なアプローチは、 <property>chains</property> パラメータを使うことです。
  63. このものは単にルートの一覧です。そして、それは親ルートでチェーンされます。
  64. 親ルートも子供ルートも、結果として生じるチェーンされたルートにだけ直接追加され、
  65. それ以外のルータには追加されません。
  66. ルータでのチェーンされたルートの名前は、
  67. デフォルトでダッシュで連結される親ルート名と子供ルート名です。
  68. <acronym>XML</acronym> での単純な構成は、このように見えます。
  69. </para>
  70. <programlisting language="xml"><![CDATA[
  71. <routes>
  72. <www type="Zend_Controller_Router_Route_Hostname">
  73. <route>www.example.com</route>
  74. <chains>
  75. <language type="Zend_Controller_Router_Route">
  76. <route>:language</route>
  77. <reqs language="[a-z]{2}">
  78. <chains>
  79. <index type="Zend_Controller_Router_Route_Static">
  80. <route></route>
  81. <defaults module="default" controller="index"
  82. action="index" />
  83. </index>
  84. <imprint type="Zend_Controller_Router_Route_Static">
  85. <route>imprint</route>
  86. <defaults module="default" controller="index"
  87. action="index" />
  88. </imprint>
  89. </chains>
  90. </language>
  91. </chains>
  92. </www>
  93. <users type="Zend_Controller_Router_Route_Hostname">
  94. <route>users.example.com</route>
  95. <chains>
  96. <profile type="Zend_Controller_Router_Route">
  97. <route>:username</route>
  98. <defaults module="users" controller="profile" action="index" />
  99. </profile>
  100. </chains>
  101. </users>
  102. <misc type="Zend_Controller_Router_Route_Static">
  103. <route>misc</route>
  104. </misc>
  105. </routes>
  106. ]]></programlisting>
  107. <para>
  108. これは結果として、ホスト名及びルート <command>misc</command> に基づいてマッチするだけで、
  109. どんなホスト名ともマッチする3つのルート、
  110. <command>www-language-index</command> 、 <command>www-language-imprint</command> 及び
  111. <command>users-language-profile</command> になります。
  112. </para>
  113. <para>
  114. チェーンされたルートを作成する別な方法は、
  115. <property>chain</property> パラメータを介することです。
  116. それはチェーン・ルート型とともにのみ直接使うことができ、
  117. さらに root レベルでのみ動作します。
  118. </para>
  119. <programlisting language="xml"><![CDATA[
  120. <routes>
  121. <www type="Zend_Controller_Router_Route_Chain">
  122. <route>www.example.com</route>
  123. </www>
  124. <language type="Zend_Controller_Router_Route">
  125. <route>:language</route>
  126. <reqs language="[a-z]{2}">
  127. </language>
  128. <index type="Zend_Controller_Router_Route_Static">
  129. <route></route>
  130. <defaults module="default" controller="index" action="index" />
  131. </index>
  132. <imprint type="Zend_Controller_Router_Route_Static">
  133. <route>imprint</route>
  134. <defaults module="default" controller="index" action="index" />
  135. </imprint>
  136. <www-index type="Zend_Controller_Router_Route_Chain">
  137. <chain>www, language, index</chain>
  138. </www-index>
  139. <www-imprint type="Zend_Controller_Router_Route_Chain">
  140. <chain>www, language, imprint</chain>
  141. </www-imprint>
  142. </routes>
  143. ]]></programlisting>
  144. <para>
  145. コンマでルートを分離する代わりに、
  146. 配列として <property>chain</property> パラメータを与えることもできます
  147. </para>
  148. <programlisting language="xml"><![CDATA[
  149. <routes>
  150. <www-index type="Zend_Controller_Router_Route_Chain">
  151. <chain>www</chain>
  152. <chain>language</chain>
  153. <chain>index</chain>
  154. </www-index>
  155. <www-imprint type="Zend_Controller_Router_Route_Chain">
  156. <chain>www</chain>
  157. <chain>language</chain>
  158. <chain>imprint</chain>
  159. </www-imprint>
  160. </routes>
  161. ]]></programlisting>
  162. <para>
  163. <classname>Zend_Config</classname> でチェーン・ルートを構成して、
  164. チェーン名の分離記号をダッシュ以外にしたい場合、
  165. この分離記号を別途指定する必要があります。
  166. </para>
  167. <programlisting language="php"><![CDATA[
  168. $config = new Zend_Config(array(
  169. 'chainName' => array(
  170. 'type' => 'Zend_Controller_Router_Route_Static',
  171. 'route' => 'foo',
  172. 'chains' => array(
  173. 'subRouteName' => array(
  174. 'type' => 'Zend_Controller_Router_Route_Static',
  175. 'route' => 'bar',
  176. 'defaults' => array(
  177. 'module' => 'module',
  178. 'controller' => 'controller',
  179. 'action' => 'action'
  180. )
  181. )
  182. )
  183. )
  184. ));
  185. //構成追加前にセパレータを設定
  186. $router->setChainNameSeparator('_separator_')
  187. //構成を追加
  188. $router->addConfig($config);
  189. //そしてルート名はこうなります: chainName_separator_subRouteName
  190. echo $this->_router->assemble(array(), 'chainName_separator_subRouteName');
  191. //検証: /foo/bar をエコーします。
  192. ]]></programlisting>
  193. </sect4>
  194. </sect3>
  195. <!--
  196. vim:se ts=4 sw=4 et:
  197. -->