Zend_Controller-QuickStart.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.controller.quickstart">
  4. <title>Zend_Controller Quick Start</title>
  5. <sect2 id="zend.controller.quickstart.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. <classname>Zend_Controller</classname> is the heart of Zend Framework's
  9. <acronym>MVC</acronym> system. <acronym>MVC</acronym> stands for <ulink
  10. url="http://en.wikipedia.org/wiki/Model-view-controller">Model-View-Controller</ulink>
  11. and is a design pattern targeted at separating application logic
  12. from display logic. <classname>Zend_Controller_Front</classname> implements a
  13. <ulink
  14. url="http://www.martinfowler.com/eaaCatalog/frontController.html">Front
  15. Controller</ulink> pattern, in which all requests are
  16. intercepted by the front controller and dispatched to individual
  17. Action Controllers based on the <acronym>URL</acronym> requested.
  18. </para>
  19. <para>
  20. The <classname>Zend_Controller</classname> system was built with extensibility
  21. in mind, either by subclassing the existing classes, writing new
  22. classes that implement the various interfaces and abstract classes
  23. that form the foundation of the controller family of classes, or
  24. writing plugins or action helpers to augment or manipulate the
  25. functionality of the system.
  26. </para>
  27. </sect2>
  28. <sect2 id="zend.controller.quickstart.go">
  29. <title>Quick Start</title>
  30. <para>
  31. If you need more in-depth information, see the following sections.
  32. If you just want to get up and running quickly, read on.
  33. </para>
  34. <sect3 id="zend.controller.quickstart.go.directory">
  35. <title>Create the Filesystem Layout</title>
  36. <para>
  37. The first step is to create your file system layout. The typical
  38. layout is as follows:
  39. </para>
  40. <programlisting language="php"><![CDATA[
  41. application/
  42. controllers/
  43. IndexController.php
  44. models/
  45. views/
  46. scripts/
  47. index/
  48. index.phtml
  49. helpers/
  50. filters/
  51. html/
  52. .htaccess
  53. index.php
  54. ]]></programlisting>
  55. </sect3>
  56. <sect3 id="zend.controller.quickstart.go.docroot">
  57. <title>Set the Document Root</title>
  58. <para>
  59. In your web server, point your document root to the
  60. <filename>html/</filename> directory of the above file system layout.
  61. </para>
  62. </sect3>
  63. <sect3 id="zend.controller.quickstart.go.rewrite">
  64. <title>Create the Rewrite Rules</title>
  65. <para>
  66. Edit the <filename>html/.htaccess</filename> file above to read as
  67. follows:
  68. </para>
  69. <programlisting language="php"><![CDATA[
  70. RewriteEngine On
  71. RewriteCond %{REQUEST_FILENAME} -s [OR]
  72. RewriteCond %{REQUEST_FILENAME} -l [OR]
  73. RewriteCond %{REQUEST_FILENAME} -d
  74. RewriteRule ^.*$ - [NC,L]
  75. RewriteRule ^.*$ index.php [NC,L]
  76. ]]></programlisting>
  77. <note>
  78. <title>Learn about mod_rewrite</title>
  79. <para>
  80. The above rewrite rules allow access to any file under your
  81. virtual host's document root. If there are files you do not
  82. want exposed in this way, you may want to be more
  83. restrictive in your rules. Go to the Apache website to
  84. <ulink
  85. url="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html">learn
  86. more about mod_rewrite</ulink>.
  87. </para>
  88. </note>
  89. <para>
  90. If using <acronym>IIS</acronym> 7.0, use the following as your rewrite
  91. configuration:
  92. </para>
  93. <programlisting language="xml"><![CDATA[
  94. <?xml version="1.0" encoding="UTF-8"?>
  95. <configuration>
  96. <system.webServer>
  97. <rewrite>
  98. <rules>
  99. <rule name="Imported Rule 1" stopProcessing="true">
  100. <match url="^.*$" />
  101. <conditions logicalGrouping="MatchAny">
  102. <add input="{REQUEST_FILENAME}"
  103. matchType="IsFile" pattern=""
  104. ignoreCase="false" />
  105. <add input="{REQUEST_FILENAME}"
  106. matchType="IsDirectory"
  107. pattern="" ignoreCase="false" />
  108. </conditions>
  109. <action type="None" />
  110. </rule>
  111. <rule name="Imported Rule 2" stopProcessing="true">
  112. <match url="^.*$" />
  113. <action type="Rewrite" url="index.php" />
  114. </rule>
  115. </rules>
  116. </rewrite>
  117. </system.webServer>
  118. </configuration>
  119. ]]></programlisting>
  120. <para>
  121. The above rules will route requests to existing resources
  122. (existing symlinks, non-empty files, or non-empty directories)
  123. accordingly, and all other requests to the front controller.
  124. </para>
  125. <note>
  126. <para>
  127. The above rewrite rules are for Apache; for examples of
  128. rewrite rules for other web servers, see the <link
  129. linkend="zend.controller.router.introduction">router
  130. documentation</link>.
  131. </para>
  132. </note>
  133. </sect3>
  134. <sect3 id="zend.controller.quickstart.go.bootstrap">
  135. <title>Create the Bootstrap File</title>
  136. <para>
  137. The bootstrap file is the page all requests are routed through
  138. -- <filename>html/index.php</filename> in this case. Open up
  139. <filename>html/index.php</filename> in the editor of your choice and add
  140. the following:
  141. </para>
  142. <programlisting language="php"><![CDATA[
  143. Zend_Controller_Front::run('/path/to/app/controllers');
  144. ]]></programlisting>
  145. <para>
  146. This will instantiate and dispatch the front controller, which
  147. will route requests to action controllers.
  148. </para>
  149. </sect3>
  150. <sect3 id="zend.controller.quickstart.go.controller">
  151. <title>Create the Default Action Controller</title>
  152. <para>
  153. Before discussing action controllers, you should first
  154. understand how requests are routed in Zend Framework. By
  155. default, the first segment of a <acronym>URL</acronym> path maps to a controller,
  156. and the second to an action. For example, given the <acronym>URL</acronym>
  157. <filename>http://framework.zend.com/roadmap/components</filename>, the
  158. path is <filename>/roadmap/components</filename>, which will map to the
  159. controller <emphasis>roadmap</emphasis> and the action
  160. <emphasis>components</emphasis>. If no action is provided, the action
  161. <emphasis>index</emphasis> is assumed, and if no controller is provided,
  162. the controller <emphasis>index</emphasis> is assumed (following the
  163. Apache convention that maps a <emphasis>DirectoryIndex</emphasis>
  164. automatically).
  165. </para>
  166. <para>
  167. <classname>Zend_Controller</classname>'s dispatcher then takes the
  168. controller value and maps it to a class. By default, it
  169. Title-cases the controller name and appends the word
  170. <emphasis>Controller</emphasis>. Thus, in our example above, the
  171. controller <emphasis>roadmap</emphasis> is mapped to the class
  172. <classname>RoadmapController</classname>.
  173. </para>
  174. <para>
  175. Similarly, the action value is mapped to a method of the
  176. controller class. By default, the value is lower-cased, and the
  177. word <emphasis>Action</emphasis> is appended. Thus, in our example
  178. above, the action <emphasis>components</emphasis> becomes
  179. <methodname>componentsAction()</methodname>, and the final method called is
  180. <methodname>RoadmapController::componentsAction()</methodname>.
  181. </para>
  182. <para>
  183. So, moving on, let's now create a default action controller and
  184. action method. As noted earlier, the default controller and
  185. action called are both <emphasis>index</emphasis>. Open the file
  186. <filename>application/controllers/IndexController.php</filename>, and
  187. enter the following:
  188. </para>
  189. <programlisting language="php"><![CDATA[
  190. /** Zend_Controller_Action */
  191. class IndexController extends Zend_Controller_Action
  192. {
  193. public function indexAction()
  194. {
  195. }
  196. }
  197. ]]></programlisting>
  198. <para>
  199. By default, the <link
  200. linkend="zend.controller.actionhelpers.viewrenderer">ViewRenderer</link>
  201. action helper is enabled. What this means is that by simply
  202. defining an action method and a corresponding view script, you
  203. will immediately get content rendered. By default,
  204. <classname>Zend_View</classname> is used as the View layer in the
  205. <acronym>MVC</acronym>. The
  206. <emphasis>ViewRenderer</emphasis> does some magic, and uses the
  207. controller name (e.g., <emphasis>index</emphasis>) and the current
  208. action name (e.g., <emphasis>index</emphasis>) to determine what
  209. template to pull. By default, templates end in the
  210. <filename>.phtml</filename> extension, so this means that, in the above
  211. example, the template <filename>index/index.phtml</filename> will be
  212. rendered. Additionally, the <emphasis>ViewRenderer</emphasis>
  213. automatically assumes that the directory <filename>views/</filename> at
  214. the same level as the controller directory will be the base view
  215. directory, and that the actual view scripts will be in the
  216. <filename>views/scripts/</filename> subdirectory. Thus, the template
  217. rendered will be found in
  218. <filename>application/views/scripts/index/index.phtml</filename>.
  219. </para>
  220. </sect3>
  221. <sect3 id="zend.controller.quickstart.go.view">
  222. <title>Create the View Script</title>
  223. <para>
  224. As mentioned <link
  225. linkend="zend.controller.quickstart.go.controller">in the
  226. previous section</link>, view scripts are found in
  227. <filename>application/views/scripts/</filename>; the view script for the
  228. default controller and action is in
  229. <filename>application/views/scripts/index/index.phtml</filename>. Create
  230. this file, and type in some <acronym>HTML</acronym>:
  231. </para>
  232. <programlisting language="php"><![CDATA[
  233. <!DOCTYPE html
  234. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  235. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  236. <html>
  237. <head>
  238. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  239. <title>My first Zend Framework App</title>
  240. </head>
  241. <body>
  242. <h1>Hello, World!</h1>
  243. </body>
  244. </html>
  245. ]]></programlisting>
  246. </sect3>
  247. <sect3 id="zend.controller.quickstart.go.errorhandler">
  248. <title>Create the Error Controller</title>
  249. <para>
  250. By default, <link
  251. linkend="zend.controller.plugins.standard.errorhandler">the
  252. error handler plugin</link> is registered. This plugin expects
  253. that a controller exists to handle errors. By default, it
  254. assumes an <emphasis>ErrorController</emphasis> in the default module
  255. with an <methodname>errorAction()</methodname> method:
  256. </para>
  257. <programlisting language="php"><![CDATA[
  258. class ErrorController extends Zend_Controller_Action
  259. {
  260. public function errorAction()
  261. {
  262. }
  263. }
  264. ]]></programlisting>
  265. <para>
  266. Assuming the already discussed directory layout, this file will
  267. go in <filename>application/controllers/ErrorController.php</filename>.
  268. You will also need to create a view script in
  269. <filename>application/views/scripts/error/error.phtml</filename>; sample
  270. content might look like:
  271. </para>
  272. <programlisting language="php"><![CDATA[
  273. <!DOCTYPE html
  274. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  275. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  276. <html>
  277. <head>
  278. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  279. <title>Error</title>
  280. </head>
  281. <body>
  282. <h1>An error occurred</h1>
  283. <p>An error occurred; please try again later.</p>
  284. </body>
  285. </html>
  286. ]]></programlisting>
  287. </sect3>
  288. <sect3 id="zend.controller.quickstart.go.finish">
  289. <title>View the Site!</title>
  290. <para>
  291. With your first controller and view under your belt, you can now
  292. fire up your browser and browse to the site. Assuming
  293. <filename>example.com</filename> is your domain, any of the following
  294. <acronym>URL</acronym>s will get to the page we've just created:
  295. </para>
  296. <itemizedlist>
  297. <listitem>
  298. <para><filename>http://example.com/</filename></para>
  299. </listitem>
  300. <listitem>
  301. <para><filename>http://example.com/index</filename></para>
  302. </listitem>
  303. <listitem>
  304. <para><filename>http://example.com/index/index</filename></para>
  305. </listitem>
  306. </itemizedlist>
  307. <para>
  308. You're now ready to start creating more controllers and action
  309. methods. Congratulations!
  310. </para>
  311. </sect3>
  312. </sect2>
  313. </sect1>
  314. <!--
  315. vim:se ts=4 sw=4 et:
  316. -->