Zend_Controller-QuickStart.xml 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 24249 -->
  4. <sect1 id="zend.controller.quickstart">
  5. <title>Zend_Controller クイックスタート</title>
  6. <sect2 id="zend.controller.quickstart.introduction">
  7. <title>導入</title>
  8. <para>
  9. <classname>Zend_Controller</classname> は、Zend Framework の <acronym>MVC</acronym>
  10. システムの中心となるものです。<acronym>MVC</acronym> は <ulink
  11. url="http://en.wikipedia.org/wiki/Model-view-controller">モデル-ビュー-コントローラ</ulink>
  12. の頭文字をとったもので、アプリケーションのロジックと表示ロジックを分離させる設計手法です。
  13. <classname>Zend_Controller_Front</classname> は
  14. <ulink
  15. url="http://www.martinfowler.com/eaaCatalog/frontController.html">
  16. フロントコントローラ</ulink> パターンを実装しており、
  17. すべてのリクエストをいったんフロントコントローラで受け取った上でその
  18. <acronym>URL</acronym> にもとづいたアクションコントローラに配送します。
  19. </para>
  20. <para>
  21. <classname>Zend_Controller</classname> は、拡張性を考慮して作成されています。
  22. 拡張の方法としては、既存のクラスのサブクラスを作成する方法と
  23. アクションヘルパーを作成する方法があります。
  24. 新しいサブクラスを作成すると、コントローラクラスの基盤となる
  25. インターフェイスや機能を新たに書くことができます。
  26. アクションヘルパーを使用すると、システムの機能を強化したり変更したりできるようになります。
  27. </para>
  28. </sect2>
  29. <sect2 id="zend.controller.quickstart.go">
  30. <title>クイックスタート</title>
  31. <para>
  32. より詳しい情報が知りたい場合は、次のセクションを参照ください。
  33. とりあえず動かしてみたいという方は、ここを読むといいでしょう。
  34. </para>
  35. <sect3 id="zend.controller.quickstart.go.directory">
  36. <title>ファイルシステムレイアウトの作成</title>
  37. <para>
  38. まずはディレクトリ構成を決めましょう。
  39. 典型的なレイアウトは、次のようなものです。
  40. </para>
  41. <programlisting language="php"><![CDATA[
  42. application/
  43. controllers/
  44. IndexController.php
  45. models/
  46. views/
  47. scripts/
  48. index/
  49. index.phtml
  50. helpers/
  51. filters/
  52. html/
  53. .htaccess
  54. index.php
  55. ]]></programlisting>
  56. </sect3>
  57. <sect3 id="zend.controller.quickstart.go.docroot">
  58. <title>ドキュメントルートの設定</title>
  59. <para>
  60. ウェブサーバのドキュメントルートを、先ほどのレイアウト中の
  61. <filename>html/</filename> ディレクトリに設定します。
  62. </para>
  63. </sect3>
  64. <sect3 id="zend.controller.quickstart.go.rewrite">
  65. <title>rewrite ルールの作成</title>
  66. <para>
  67. 上のレイアウトの <filename>html/.htaccess</filename>
  68. ファイルを、次のように編集します。
  69. </para>
  70. <programlisting language="php"><![CDATA[
  71. RewriteEngine On
  72. RewriteCond %{REQUEST_FILENAME} -s [OR]
  73. RewriteCond %{REQUEST_FILENAME} -l [OR]
  74. RewriteCond %{REQUEST_FILENAME} -d
  75. RewriteRule ^.*$ - [NC,L]
  76. RewriteRule ^.*$ index.php [NC,L]
  77. ]]></programlisting>
  78. <note>
  79. <title>mod_rewrite について</title>
  80. <para>
  81. 上のリライトルールは、バーチャルホストのドキュメントルート配下にある
  82. すべてのファイルへのアクセスを許可するものです。
  83. この方式で公開してしまってはまずいファイルがある場合は、
  84. このルールにさらに制約を追加しなければなりません。
  85. Apache のウェブサイトにいけば、
  86. <ulink
  87. url="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html">
  88. mod_rewrite について</ulink>
  89. もっと詳しく知ることができます。
  90. </para>
  91. </note>
  92. <para>
  93. <acronym>IIS</acronym> 7.0 をお使いの場合は、次のような rewrite 設定を使用します。
  94. </para>
  95. <programlisting language="xml"><![CDATA[
  96. <?xml version="1.0" encoding="UTF-8"?>
  97. <configuration>
  98. <system.webServer>
  99. <rewrite>
  100. <rules>
  101. <rule name="Imported Rule 1" stopProcessing="true">
  102. <match url="^.*$" />
  103. <conditions logicalGrouping="MatchAny">
  104. <add input="{REQUEST_FILENAME}"
  105. matchType="IsFile" pattern=""
  106. ignoreCase="false" />
  107. <add input="{REQUEST_FILENAME}"
  108. matchType="IsDirectory"
  109. pattern="" ignoreCase="false" />
  110. </conditions>
  111. <action type="None" />
  112. </rule>
  113. <rule name="Imported Rule 2" stopProcessing="true">
  114. <match url="^.*$" />
  115. <action type="Rewrite" url="index.php" />
  116. </rule>
  117. </rules>
  118. </rewrite>
  119. </system.webServer>
  120. </configuration>
  121. ]]></programlisting>
  122. <para>
  123. このルールは、既存のリソース
  124. (シンボリックリンク、空でないファイル、あるいは空でないディレクトリ)
  125. へのリクエストを適切に転送し、
  126. それ以外のすべてのリクエストをフロントコントローラに転送します。
  127. </para>
  128. <note>
  129. <para>
  130. 上の rewrire ルールは Apache 用のものです。
  131. その他のウェブサーバ用の例については
  132. <link linkend="zend.controller.router.introduction">
  133. ルータのドキュメント</link> を参照ください。
  134. </para>
  135. </note>
  136. </sect3>
  137. <sect3 id="zend.controller.quickstart.go.bootstrap">
  138. <title>起動ファイルの作成</title>
  139. <para>
  140. 起動ファイルとはすべてのリクエストの転送先となるファイルのことで、
  141. 今回の例では <filename>html/index.php</filename> がそれにあたります。
  142. <filename>html/index.php</filename> をお好みのエディタで開き、次の内容を追加します。
  143. </para>
  144. <programlisting language="php"><![CDATA[
  145. Zend_Controller_Front::run('/path/to/app/controllers');
  146. ]]></programlisting>
  147. <para>
  148. これは、フロントコントローラのインスタンスとディスパッチを行います。
  149. この結果、アクションコントローラへリクエストが転送されます。
  150. </para>
  151. </sect3>
  152. <sect3 id="zend.controller.quickstart.go.controller">
  153. <title>デフォルトのアクションコントローラの作成</title>
  154. <para>
  155. アクションコントローラについて説明する前に、まず
  156. Zend Framework でのリクエストの処理方法について知っておきましょう。
  157. デフォルトでは、<acronym>URL</acronym> パスの最初の部分がコントローラ、
  158. そしてその次の部分がアクションに対応します。たとえば <acronym>URL</acronym> が
  159. <filename>http://framework.zend.com/roadmap/components</filename>
  160. である場合、パスは <filename>/roadmap/components</filename>
  161. となり、これは <emphasis>roadmap</emphasis> コントローラの
  162. <emphasis>components</emphasis> アクションに対応します。
  163. アクションを省略した場合は <emphasis>index</emphasis> アクションであるとみなされます。
  164. またコントローラを省略した場合は <emphasis>index</emphasis> コントローラであるとみなされます
  165. (Apache が自動的に <emphasis>DirectoryIndex</emphasis>
  166. に対応させるという規約に従っています)。
  167. </para>
  168. <para>
  169. <classname>Zend_Controller</classname> のディスパッチャは、
  170. コントローラを対応するクラスに関連付けます。
  171. デフォルトでは、コントローラ名の先頭を大文字にしたものに
  172. <emphasis>Controller</emphasis> をつなげたものがクラス名となります。
  173. つまり、上の例では <emphasis>roadmap</emphasis> コントローラが
  174. <emphasis>RoadmapController</emphasis> クラスに対応することになります。
  175. </para>
  176. <para>
  177. 同様に、アクションもコントローラクラスのメソッドに関連付けます。
  178. デフォルトでは、アクション名を小文字に変換して
  179. <emphasis>Action</emphasis> を追加したものがメソッド名となります。
  180. つまり、上の例では <emphasis>components</emphasis> アクションは
  181. <methodname>componentsAction()</methodname> メソッドになり、最終的に
  182. <methodname>RoadmapController::componentsAction()</methodname>
  183. がコールされることになります。
  184. </para>
  185. <para>
  186. 続いて、デフォルトのアクションコントローラと
  187. アクションメソッドを作ってみましょう。
  188. 先ほど説明したように、デフォルトのコントローラ名およびアクション名はどちらも
  189. <emphasis>index</emphasis> となります。
  190. <filename>application/controllers/IndexController.php</filename>
  191. を開き、次の内容を入力しましょう。
  192. </para>
  193. <programlisting language="php"><![CDATA[
  194. /** Zend_Controller_Action */
  195. class IndexController extends Zend_Controller_Action
  196. {
  197. public function indexAction()
  198. {
  199. }
  200. }
  201. ]]></programlisting>
  202. <para>
  203. デフォルトでは
  204. <link linkend="zend.controller.actionhelpers.viewrenderer">ViewRenderer</link>
  205. アクションヘルパーが有効になります。つまり、
  206. アクションメソッドとそれに対応するビュースクリプトを用意すれば、
  207. すぐにその内容をレンダリングできるというわけです。
  208. デフォルトでは、<acronym>MVC</acronym> のビュー層として <classname>Zend_View</classname> を使用します。
  209. <emphasis>ViewRenderer</emphasis> は、コントローラ名
  210. (たとえば <emphasis>index</emphasis>) とアクション名
  211. (たとえば <emphasis>index</emphasis>) から処理するテンプレートを決定します。
  212. デフォルトでは、テンプレートの拡張子は
  213. <filename>.phtml</filename> となります。つまり、上の例では
  214. <filename>index/index.phtml</filename> をレンダリングします。
  215. さらに <emphasis>ViewRenderer</emphasis> は、
  216. コントローラと同一階層にある <filename>views/</filename>
  217. ディレクトリを自動的にビューの基底ディレクトリとみなし、
  218. <filename>views/scripts/</filename> ビュースクリプトがおかれるものと考えます。
  219. したがって、実際にレンダリングされるテンプレートは
  220. <filename>application/views/scripts/index/index.phtml</filename>
  221. となります。
  222. </para>
  223. </sect3>
  224. <sect3 id="zend.controller.quickstart.go.view">
  225. <title>ビュースクリプトの作成</title>
  226. <para>
  227. <link linkend="zend.controller.quickstart.go.controller">
  228. 先ほど説明したように</link>、ビュースクリプトの場所は
  229. <filename>application/views/scripts/</filename> です。
  230. デフォルトコントローラにおけるデフォルトのアクションのビュースクリプトは
  231. <filename>application/views/scripts/index/index.phtml</filename>
  232. となります。このファイルを作成し、何か <acronym>HTML</acronym> を入力してみましょう。
  233. </para>
  234. <programlisting language="php"><![CDATA[
  235. <!DOCTYPE html
  236. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  237. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  238. <html>
  239. <head>
  240. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  241. <title>My first Zend Framework App</title>
  242. </head>
  243. <body>
  244. <h1>Hello, World!</h1>
  245. </body>
  246. </html>
  247. ]]></programlisting>
  248. </sect3>
  249. <sect3 id="zend.controller.quickstart.go.errorhandler">
  250. <title>エラーコントローラの作成</title>
  251. <para>
  252. デフォルトで、
  253. <link linkend="zend.controller.plugins.standard.errorhandler">
  254. エラーハンドラプラグイン</link> が登録されています。
  255. このプラグインを使用するには、エラー処理用のコントローラが必要です。
  256. デフォルト設定では、デフォルトモジュールの
  257. <emphasis>ErrorController</emphasis> に <methodname>errorAction()</methodname>
  258. というメソッドがあることを想定しています。
  259. </para>
  260. <programlisting language="php"><![CDATA[
  261. class ErrorController extends Zend_Controller_Action
  262. {
  263. public function errorAction()
  264. {
  265. }
  266. }
  267. ]]></programlisting>
  268. <para>
  269. 先ほど説明したディレクトリ構成により、このファイルは
  270. <filename>application/controllers/ErrorController.php</filename>
  271. に配置されることになります。これとは別に、ビュースクリプト
  272. <filename>application/views/scripts/error/error.phtml</filename>
  273. が必要です。その中身は、たとえば次のようになるでしょう。
  274. </para>
  275. <programlisting language="php"><![CDATA[
  276. <!DOCTYPE html
  277. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  278. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  279. <html>
  280. <head>
  281. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  282. <title>エラー</title>
  283. </head>
  284. <body>
  285. <h1>エラーが発生しました</h1>
  286. <p>エラーが発生しました。後ほどもう一度お試しください。</p>
  287. </body>
  288. </html>
  289. ]]></programlisting>
  290. </sect3>
  291. <sect3 id="zend.controller.quickstart.go.finish">
  292. <title>実際に見てみましょう!</title>
  293. <para>
  294. ここまでくれば、実際にブラウザでサイトを表示してみることができます。
  295. あなたのドメインが <filename>example.com</filename> だとすると、
  296. 以下のいずれかの <acronym>URL</acronym> で先ほど作成したページが表示されることでしょう。
  297. </para>
  298. <itemizedlist>
  299. <listitem>
  300. <para><filename>http://example.com/</filename></para>
  301. </listitem>
  302. <listitem>
  303. <para><filename>http://example.com/index</filename></para>
  304. </listitem>
  305. <listitem>
  306. <para><filename>http://example.com/index/index</filename></para>
  307. </listitem>
  308. </itemizedlist>
  309. <para>
  310. これで、実際にコントローラやアクションを作成する準備ができました。
  311. おめでとうございます!
  312. </para>
  313. </sect3>
  314. </sect2>
  315. </sect1>
  316. <!--
  317. vim:se ts=4 sw=4 et:
  318. -->