Zend_Controller-Dispatcher.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 24249 -->
  4. <sect1 id="zend.controller.dispatcher">
  5. <title>ディスパッチャ</title>
  6. <sect2 id="zend.controller.dispatcher.overview">
  7. <title>概要</title>
  8. <para>
  9. ディスパッチ処理は、リクエストオブジェクトである
  10. <classname>Zend_Controller_Request_Abstract</classname> を受け取り、
  11. そこに含まれる情報 (モジュール名、コントローラ名、アクション名およびオプションのパラメータ)
  12. を展開し、コントローラのインスタンスを作成してそのコントローラのアクションをコールします。
  13. モジュールやコントローラ、アクションが見つからない場合は、
  14. デフォルト値を使用します。<classname>Zend_Controller_Dispatcher_Standard</classname>
  15. では、コントローラとアクションのデフォルトはどちらも
  16. <emphasis>index</emphasis> で、モジュールのデフォルトは <emphasis>default</emphasis> です。しかし、
  17. <methodname>setDefaultController()</methodname> メソッドや
  18. <methodname>setDefaultAction()</methodname> メソッド、そして
  19. <methodname>setDefaultModule()</methodname> でこれらを変更することもできます。
  20. </para>
  21. <note>
  22. <title>デフォルトモジュール</title>
  23. <para>
  24. モジュール構造のアプリケーションを作成する場合に、
  25. デフォルトのモジュールにも名前空間を定義したくなることもあるでしょう
  26. (デフォルトの設定では、デフォルトモジュールには名前空間が
  27. <emphasis>ありません</emphasis>)。1.5.0 以降では、
  28. フロントコントローラあるいはディスパッチャで
  29. <property>prefixDefaultModule</property> に <constant>TRUE</constant>
  30. を設定すればこれが実現できるようになりました。
  31. </para>
  32. <programlisting language="php"><![CDATA[
  33. // フロントコントローラで
  34. $front->setParam('prefixDefaultModule', true);
  35. // ディスパッチャで
  36. $dispatcher->setParam('prefixDefaultModule', true);
  37. ]]></programlisting>
  38. <para>
  39. これにより、既存のモジュールをアプリケーションのデフォルトモジュールとすることができます。
  40. </para>
  41. </note>
  42. <para>
  43. ディスパッチ処理が発生するのは、フロントコントローラでのループの内部です。
  44. ディスパッチ処理を行う前に、フロントコントローラはルーティングを行い、
  45. ユーザが指定したコントローラとアクション、そして追加のパラメータを取得します。
  46. それからディスパッチループに入り、リクエストを配送します。
  47. </para>
  48. <para>
  49. ループ内では、まず最初にリクエストオブジェクトのフラグを設定します。
  50. このフラグは、アクションがディスパッチされたことを示すものです。
  51. アクション内や pre/postDispatch プラグインでこのフラグをリセットすると、
  52. ディスパッチループがそのまま継続され、もう一度リクエストを処理しようとします。
  53. リクエスト内のコントローラやアクションを変更してフラグをリセットすることで、
  54. さまざまなリクエストを続けて実行させることができます。
  55. </para>
  56. <para>
  57. このようなディスパッチ処理を制御する
  58. アクションコントローラのメソッドが <methodname>_forward()</methodname> です。
  59. このメソッドを <methodname>preDispatch()</methodname>、
  60. <methodname>postDispatch()</methodname> やアクションメソッドでコールし、
  61. コントローラやアクション、
  62. そして新しいアクションに送りたい追加のパラメータを指定します。
  63. </para>
  64. <programlisting language="php"><![CDATA[
  65. public function fooAction()
  66. {
  67. // 現在のモジュールおよびコントローラの、別のアクションに転送します
  68. $this->_forward('bar', null, null, array('baz' => 'bogus'));
  69. }
  70. public function barAction()
  71. {
  72. // 現在のモジュールにある、別のコントローラのアクション
  73. // FooController::bazAction() に転送します
  74. $this->_forward('baz', 'foo', null, array('baz' => 'bogus'));
  75. }
  76. public function bazAction()
  77. {
  78. // 別のモジュールにある、別のコントローラのアクション
  79. // Foo_BarController::bazAction() に転送します
  80. $this->_forward('baz', 'bar', 'foo', array('baz' => 'bogus'));
  81. }
  82. ]]></programlisting>
  83. </sect2>
  84. <sect2 id="zend.controller.dispatcher.subclassing">
  85. <title>ディスパッチャのサブクラスの作成</title>
  86. <para>
  87. <classname>Zend_Controller_Front</classname> は、
  88. まず最初にルータをコールして、
  89. リクエスト内で最初にディスパッチできるアクションを決定します。
  90. その後、ディスパッチャループに入り、ディスパッチャをコールしてアクションを振り分けます。
  91. </para>
  92. <para>
  93. ディスパッチャが動作するためには、さまざまなデータが必要です。
  94. たとえば、コントローラ名やアクション名を決定する方法、
  95. コントローラクラスを探す場所、モジュール名が有効かどうか、
  96. その他、リクエストの内容をディスパッチするために必要な情報を取得する
  97. <acronym>API</acronym> が必要となります。
  98. </para>
  99. <para>
  100. <classname>Zend_Controller_Dispatcher_Interface</classname>
  101. では次のようなメソッドを定義しています。ディスパッチャは、これを実装しなければなりません。
  102. </para>
  103. <programlisting language="php"><![CDATA[
  104. interface Zend_Controller_Dispatcher_Interface
  105. {
  106. /**
  107. * Format a string into a controller class name.
  108. *
  109. * @param string $unformatted
  110. * @return string
  111. */
  112. public function formatControllerName($unformatted);
  113. /**
  114. * Format a string into an action method name.
  115. *
  116. * @param string $unformatted
  117. * @return string
  118. */
  119. public function formatActionName($unformatted);
  120. /**
  121. * Determine if a request is dispatchable
  122. *
  123. * @param Zend_Controller_Request_Abstract $request
  124. * @return boolean
  125. */
  126. public function isDispatchable(
  127. Zend_Controller_Request_Abstract $request
  128. );
  129. /**
  130. * Set a user parameter (via front controller, or for local use)
  131. *
  132. * @param string $name
  133. * @param mixed $value
  134. * @return Zend_Controller_Dispatcher_Interface
  135. */
  136. public function setParam($name, $value);
  137. /**
  138. * Set an array of user parameters
  139. *
  140. * @param array $params
  141. * @return Zend_Controller_Dispatcher_Interface
  142. */
  143. public function setParams(array $params);
  144. /**
  145. * Retrieve a single user parameter
  146. *
  147. * @param string $name
  148. * @return mixed
  149. */
  150. public function getParam($name);
  151. /**
  152. * Retrieve all user parameters
  153. *
  154. * @return array
  155. */
  156. public function getParams();
  157. /**
  158. * Clear the user parameter stack, or a single user parameter
  159. *
  160. * @param null|string|array single key or array of keys for
  161. * params to clear
  162. * @return Zend_Controller_Dispatcher_Interface
  163. */
  164. public function clearParams($name = null);
  165. /**
  166. * Set the response object to use, if any
  167. *
  168. * @param Zend_Controller_Response_Abstract|null $response
  169. * @return void
  170. */
  171. public function setResponse(
  172. Zend_Controller_Response_Abstract $response = null
  173. );
  174. /**
  175. * Retrieve the response object, if any
  176. *
  177. * @return Zend_Controller_Response_Abstract|null
  178. */
  179. public function getResponse();
  180. /**
  181. * Add a controller directory to the controller directory stack
  182. *
  183. * @param string $path
  184. * @param string $args
  185. * @return Zend_Controller_Dispatcher_Interface
  186. */
  187. public function addControllerDirectory($path, $args = null);
  188. /**
  189. * Set the directory (or directories) where controller files are
  190. * stored
  191. *
  192. * @param string|array $dir
  193. * @return Zend_Controller_Dispatcher_Interface
  194. */
  195. public function setControllerDirectory($path);
  196. /**
  197. * Return the currently set directory(ies) for controller file
  198. * lookup
  199. *
  200. * @return array
  201. */
  202. public function getControllerDirectory();
  203. /**
  204. * Dispatch a request to a (module/)controller/action.
  205. *
  206. * @param Zend_Controller_Request_Abstract $request
  207. * @param Zend_Controller_Response_Abstract $response
  208. * @return Zend_Controller_Request_Abstract|boolean
  209. */
  210. public function dispatch(
  211. Zend_Controller_Request_Abstract $request,
  212. Zend_Controller_Response_Abstract $response
  213. );
  214. /**
  215. * Whether or not a given module is valid
  216. *
  217. * @param string $module
  218. * @return boolean
  219. */
  220. public function isValidModule($module);
  221. /**
  222. * Retrieve the default module name
  223. *
  224. * @return string
  225. */
  226. public function getDefaultModule();
  227. /**
  228. * Retrieve the default controller name
  229. *
  230. * @return string
  231. */
  232. public function getDefaultControllerName();
  233. /**
  234. * Retrieve the default action
  235. *
  236. * @return string
  237. */
  238. public function getDefaultAction();
  239. }
  240. ]]></programlisting>
  241. <para>
  242. しかし、たいていの場合は単純に抽象クラス
  243. <classname>Zend_Controller_Dispatcher_Abstract</classname>
  244. を継承するだけで事足りるでしょう。ここには、これらのメソッドがすでに定義されています。
  245. あるいは、<classname>Zend_Controller_Dispatcher_Standard</classname>
  246. を継承して、標準の機能と異なる部分だけを変更するということも可能です。
  247. </para>
  248. <para>
  249. ディスパッチャのサブクラスを作成する必要がある場面としては、
  250. たとえばアクションコントローラ内で
  251. 標準とは異なるクラス名やメソッド名の命名規則を使用したいなどということが考えられます。
  252. あるいは、クラスメソッドに振り分けるのではなく
  253. コントローラディレクトリは以下のアクションファイルに振り分けるなど、
  254. 異なるディスパッチ方式を使用したい場合にもサブクラスを作成する必要があります。
  255. </para>
  256. </sect2>
  257. </sect1>
  258. <!--
  259. vim:se ts=4 sw=4 et:
  260. -->