Zend_Controller-QuickStart.xml 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <sect1 id="zend.controller.quickstart">
  2. <title>Zend_Controller 快速入门</title>
  3. <sect2 id="zend.controller.quickstart.introduction">
  4. <title>简介</title>
  5. <para>
  6. <code>Zend_Controller</code>是Zend Framework的MVC体系的核心部份。MVC指<ulink
  7. url="http://en.wikipedia.org/wiki/Model-view-controller">Model-View-Controller</ulink>,是一个用于分离应用逻辑和表现逻辑的设计模式。<code>Zend_Controller_Front</code>实现了<ulink
  8. url="http://www.martinfowler.com/eaaCatalog/frontController.html">Front Controller</ulink>设计模式,所有的请求都通过front controller(前端控制器)并分发(dispatch)到不同的控制器来处理,分发的过程基于请求的URL。
  9. </para>
  10. <para>
  11. <code>Zend_Controller</code>体系具有可扩展性,可以通过继承已有的类或者自己写个新的类来实现各种接口和抽象类,也可以编写插件或者助手类(helper)来增强系统的功能。
  12. </para>
  13. </sect2>
  14. <sect2 id="zend.controller.quickstart.go">
  15. <title>入门</title>
  16. <para>
  17. 如果你想深入了解控制器,请认真看下面几节内容,如果你只是想快速地把系统搭建起来,看这一节。
  18. </para>
  19. <sect3 id="zend.controller.quickstart.go.directory">
  20. <title>文件系统的组织结构</title>
  21. <para>
  22. 首先你要规划好自己的文件结构布局,典型的结构是这样的:
  23. </para>
  24. <programlisting role="php"><![CDATA[
  25. application/
  26. controllers/
  27. IndexController.php
  28. models/
  29. views/
  30. scripts/
  31. index/
  32. index.phtml
  33. helpers/
  34. filters/
  35. html/
  36. .htaccess
  37. index.php
  38. ]]>
  39. </programlisting>
  40. </sect3>
  41. <sect3 id="zend.controller.quickstart.go.docroot">
  42. <title>设置文件根目录</title>
  43. <para>
  44. 将你的WEB服务器文件根目录指向上面文件布局中的<code>html</code>目录。
  45. </para>
  46. </sect3>
  47. <sect3 id="zend.controller.quickstart.go.rewrite">
  48. <title>创建URL重写规则</title>
  49. <para>
  50. 创建<code>html/.htaccess</code>,编辑其内容为:
  51. </para>
  52. <programlisting role="php"><![CDATA[
  53. RewriteEngine on
  54. RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
  55. ]]>
  56. </programlisting>
  57. <para>
  58. 在上例中,所有请求(除了对那几个扩展名的文件的请求)都被转向到index.php。如果你想在访问某些扩展名的文件时不转向到index.php,你可以建立自己的URL重写规则,或者直接把扩展名添加到(|)之中去。
  59. </para>
  60. <note>
  61. <para>
  62. 上面的重写规则只能用于Apache服务器,如果你使用的是其它服务器,请查看 <link linkend="zend.controller.router.introduction">router的文档</link>.
  63. </para>
  64. </note>
  65. </sect3>
  66. <sect3 id="zend.controller.quickstart.go.bootstrap">
  67. <title>创建你的bootstrap文件</title>
  68. <para>
  69. (所谓Bootstrap,就相当于一小段引导程序或者入口程序,在ZF中,通常即指index.php,因为我们所有的请求都是从index.php进入的。--Haohappy注)
  70. 在设置好 .htaccess文件之后,建立一个新的文件index.php:
  71. </para>
  72. <programlisting role="php"><![CDATA[
  73. Zend_Controller_Front::run('/path/to/app/controllers');
  74. ]]>
  75. </programlisting>
  76. <para>
  77. 这是一个入口文件,目的仅在于启动Zend_Controller_Front。Zend_Controller_Front用于分配请求到不同的控制器。
  78. </para>
  79. </sect3>
  80. <sect3 id="zend.controller.quickstart.go.controller">
  81. <title>创建默认的控制器(Action Controller)</title>
  82. <para>
  83. 在讨论控制器之前,你应该先理解Zend Framework是如何处理HTTP请求的。默认情况下,URL的第一个部份会映射到一个控制器,第二个部份则映射到控制器类中的Action(即控制器类内部的一个方法)。例如:URL<code>http://framework.zend.com/roadmap/components</code>,其服务器路径为<code>/roadmap/components</code>,则会映射到<code>roadmap</code>控制器和<code>components</code> Action。如果不存在action,则会调用index这个action。如果控制器不存在,则会自动调用index控制器。(按照Apache的命名惯例,自动映射到<code>DirectoryIndex</code>文件)
  84. </para>
  85. <para>
  86. 接下来,<code>Zend_Controller</code>的dispatcher会根据控制器的名称找到具体的控制器类。通常它会把控制器名称加上<code>Controller</code>。因此,上例中<code>roadmap</code>控制器与类<code>RoadmapController</code>相对应。
  87. </para>
  88. <para>
  89. 类似地,action会映射到控制器类中的一个类方法。默认情下,会被转成小写字母,然后加上<code>Action</code>字符串。因此,上例中<code>components</code>这个action与 <code>componentsAction</code>相对应。最终我们访问URL调用的是<code>RoadmapController->componentsAction()</code>
  90. </para>
  91. <para>
  92. 现在让我们来创建一个默认的控制器和Action方法吧。上面说过,默认的控制器和方法都以“index”命名。打开文件<code>application/controllers/IndexController.php</code>,输入:
  93. </para>
  94. <programlisting role="php"><![CDATA[
  95. /** Zend_Controller_Action */
  96. class IndexController extends Zend_Controller_Action
  97. {
  98. public function indexAction()
  99. {
  100. }
  101. }
  102. ]]>
  103. </programlisting>
  104. <para>
  105. 默认情况下,<link
  106. linkend="zend.controller.actionhelpers.viewrenderer">ViewRenderer</link>会被启用。这意味着只需要简单地下定义一个action方法,就会自动指定一个对应的View脚本,你可在action中直接把内容输出。默认情况下,我们采用Zend_View作为MVC开发的表现层部份。<code>ViewRenderer</code>根据控制器的名称(例如index)和当前的action的名称(例如index)来决定使用哪个模板文件。默认情况下,模板文件使用.phtml作为扩展名。 也就是说,上例中,我们将使用<code>index/index.phtml</code>模板文件。 另外,<code>ViewRenderer</code>自动假设<code>views</code>目录与控制器目录平级,作为视图层的基础目录,而实际的模板文件则放置在<code>views/scripts/</code> 子目录下。因为我们的模板文件应该是 <code>application/views/scripts/index/index.phtml</code>。
  107. </para>
  108. </sect3>
  109. <sect3 id="zend.controller.quickstart.go.view">
  110. <title>创建你的视图脚本</title>
  111. <para>
  112. 之前说过,视图脚本(即模板文件)放在<code>application/views/scripts/</code>目录下,而默认的模板是<code>application/views/scripts/index/index.phtml</code>。我们来创建这个文件,加入一些HTML代码:
  113. </para>
  114. <programlisting role="php"><![CDATA[
  115. <!DOCTYPE html
  116. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  117. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  118. <html>
  119. <head>
  120. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  121. <title>My first Zend Framework App</title>
  122. </head>
  123. <body>
  124. <h1>Hello, World!</h1>
  125. </body>
  126. </html>
  127. ]]>
  128. </programlisting>
  129. </sect3>
  130. <sect3 id="zend.controller.quickstart.go.errorhandler">
  131. <title>创建你自己的错误控制器</title>
  132. <para>
  133. 默认情况下,<link linkend="zend.controller.plugins.standard.errorhandler">“错误处理器”插件</link> 是已经被注册的。这个插件要求存在一个用于处理错误的控制器。它假设存在一个<code>ErrorController</code>,并且其中有一个名为<code>errorAction</code>的Action:
  134. </para>
  135. <programlisting role="php"><![CDATA[
  136. class ErrorController extends Zend_Controller_Action
  137. {
  138. public function errorAction()
  139. {
  140. }
  141. }
  142. ]]>
  143. </programlisting>
  144. <para>
  145. 使用我们之前讨论的目录布局,这个文件将是<code>application/controllers/ErrorController.php</code>,你也需要为它创建一个模板文件,即<code>application/views/scripts/error/error.phtml</code>。其中的内容如下:
  146. </para>
  147. <programlisting role="php"><![CDATA[
  148. <!DOCTYPE html
  149. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  150. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  151. <html>
  152. <head>
  153. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  154. <title>错误</title>
  155. </head>
  156. <body>
  157. <h1>出错了</h1>
  158. <p>出现了一个错误,请重试。</p>
  159. </body>
  160. </html>
  161. ]]>
  162. </programlisting>
  163. </sect3>
  164. <sect3 id="zend.controller.quickstart.go.finish">
  165. <title>访问站点!</title>
  166. <para>
  167. 创建好了你的第一个控制器和模板,你现在可以打开浏览器来访问看看了! 假设<code>example.com</code>是你的站点域名,那么下面的URL都会带你访问你刚才创建的页面:
  168. </para>
  169. <itemizedlist>
  170. <listitem><para><code>http://example.com/</code></para></listitem>
  171. <listitem><para><code>http://example.com/index</code></para></listitem>
  172. <listitem><para><code>http://example.com/index/index</code></para></listitem>
  173. </itemizedlist>
  174. <para>
  175. 恭喜你! 你已经准备好了,可以开始创建更多的控制器和Action!
  176. </para>
  177. </sect3>
  178. </sect2>
  179. </sect1>
  180. <!--
  181. vim:se ts=4 sw=4 et:
  182. -->