Zend_Controller-Dispatcher.xml 11 KB

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