Zend_Controller-QuickStart.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <sect1 id="zend.controller.quickstart">
  2. <title>Szybki start z klasą Zend_Controller</title>
  3. <sect2 id="zend.controller.quickstart.introduction">
  4. <title>Wprowadzenie</title>
  5. <para>
  6. Klasa <code>Zend_Controller</code> jest sercem systemu MVC
  7. biblioteki Zend Framework. MVC oznacza <ulink
  8. url="http://pl.wikipedia.org/wiki/Model-widok-kontroler">Model-Widok-Kontroler</ulink>
  9. i jest wzorcem projektowym mającym na celu oddzielenie logiki
  10. aplikacji od logiki wyświetlania. Klasa
  11. <code>Zend_Controller_Front</code> implementuje wzorzec projektowy
  12. <ulink
  13. url="http://www.martinfowler.com/eaaCatalog/frontController.html">kontrolera
  14. frontowego</ulink>. Wszystkie żądania są przechwytywane
  15. przez kontroler frontowy i na podstawie adresu URL uruchamiany
  16. jest odpowiedni kontroler akcji.
  17. </para>
  18. <para>
  19. The <code>Zend_Controller</code> system was built with extensibility
  20. in mind, either by subclassing the existing classes, writing new
  21. classes that implement the various interfaces and abstract classes
  22. that form the foundation of the controller family of classes, or
  23. writing plugins or action helpers to augment or manipulate the
  24. functionality of the system.
  25. </para>
  26. </sect2>
  27. <sect2 id="zend.controller.quickstart.go">
  28. <title>Szybki start</title>
  29. <para>
  30. If you need more in-depth information, see the following sections.
  31. If you just want to get up and running quickly, read on.
  32. </para>
  33. <sect3 id="zend.controller.quickstart.go.directory">
  34. <title>Utwórz strukturę katalogów</title>
  35. <para>
  36. Pierwszym krokiem jest utworzenie struktury katalogów. Typowa
  37. struktura wygląda w taki sposób:
  38. </para>
  39. <programlisting role="php"><![CDATA[
  40. application/
  41. controllers/
  42. IndexController.php
  43. models/
  44. views/
  45. scripts/
  46. index/
  47. index.phtml
  48. helpers/
  49. filters/
  50. html/
  51. .htaccess
  52. index.php
  53. ]]>
  54. </programlisting>
  55. </sect3>
  56. <sect3 id="zend.controller.quickstart.go.docroot">
  57. <title>Ustaw główną ścieżkę serwera</title>
  58. <para>
  59. W swoim serwerze www, ustaw główną ścieżkę serwera na katalog
  60. <code>html</code> znajdujący się w powyższej przykładowej
  61. strukturze katalogów.
  62. </para>
  63. </sect3>
  64. <sect3 id="zend.controller.quickstart.go.rewrite">
  65. <title>Ustaw reguły przepisania</title>
  66. <para>
  67. Zedytuj plik <code>html/.htaccess</code> aby wyglądał w taki
  68. sposób:
  69. </para>
  70. <programlisting role="php"><![CDATA[
  71. RewriteEngine on
  72. RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
  73. ]]>
  74. </programlisting>
  75. <para>
  76. The above rules will route any non-resource (images,
  77. stylesheets) requests to the front controller. If there are
  78. other extensions you wish to exclude from the front controller
  79. (PDFs, text files, etc), add their extensions to the switch, or
  80. create your own rewrite rules.
  81. </para>
  82. <note>
  83. <para>
  84. Powyższe reguły przepisania przygotowane sa dla serwera
  85. Apache; aby zobaczyć przykładowe reguły dla innych serwerów,
  86. zobacz <link
  87. linkend="zend.controller.router.introduction">dokumentację
  88. routera</link>.
  89. </para>
  90. </note>
  91. </sect3>
  92. <sect3 id="zend.controller.quickstart.go.bootstrap">
  93. <title>Utwórz plik ładujący</title>
  94. <para>
  95. Plik ładujący jest miejscem, przez którze przechodzą wszystkie
  96. żądania -- w tym przypadku jest to -- <code>html/index.php</code>.
  97. Otwórz plik <code>html/index.php</code> w dowolnym edytorze i
  98. dodaj poniższy kod:
  99. </para>
  100. <programlisting role="php"><![CDATA[
  101. Zend_Controller_Front::run('/path/to/app/controllers');
  102. ]]>
  103. </programlisting>
  104. <para>
  105. Utworzy to egzemplarz kontrolera frontowego i go uruchomi,
  106. co spowoduje przekazanie żądań do kontrolerów akcji.
  107. </para>
  108. </sect3>
  109. <sect3 id="zend.controller.quickstart.go.controller">
  110. <title>Utwórz domyślny kontroler akcji</title>
  111. <para>
  112. Before discussing action controllers, you should first
  113. understand how requests are routed in Zend Framework. By
  114. default, the first segment of a URL path maps to a controller,
  115. and the second to an action. For example, given the URL
  116. <code>http://framework.zend.com/roadmap/components</code>, the
  117. path is <code>/roadmap/components</code>, which will map to the
  118. controller <code>roadmap</code> and the action
  119. <code>components</code>. If no action is provided, the action
  120. <code>index</code> is assumed, and if no controller is provided,
  121. the controller <code>index</code> is assumed (following the
  122. Apache convention that maps a <code>DirectoryIndex</code>
  123. automatically).
  124. </para>
  125. <para>
  126. <code>Zend_Controller</code>'s dispatcher then takes the
  127. controller value and maps it to a class. By default, it
  128. Title-cases the controller name and appends the word
  129. <code>Controller</code>. Thus, in our example above, the
  130. controller <code>roadmap</code> is mapped to the class
  131. <code>RoadmapController</code>.
  132. </para>
  133. <para>
  134. Similarly, the action value is mapped to a method of the
  135. controller class. By default, the value is lower-cased, and the
  136. word <code>Action</code> is appended. Thus, in our example
  137. above, the action <code>components</code> becomes
  138. <code>componentsAction</code>, and the final method called is
  139. <code>RoadmapController::componentsAction()</code>.
  140. </para>
  141. <para>
  142. Idąc dalej, utwórzmy teraz domyślny kontroler akcji i metodę
  143. akcji. Jak wspomniano wcześniej, domyślna nazwa kontrolera oraz
  144. akcji to <code>index</code>. Otwórz w edytorze plik
  145. <code>application/controllers/IndexController.php</code>, i
  146. dodaj poniższy kod:
  147. </para>
  148. <programlisting role="php"><![CDATA[
  149. class IndexController extends Zend_Controller_Action
  150. {
  151. public function indexAction()
  152. {
  153. }
  154. }
  155. ]]>
  156. </programlisting>
  157. <para>
  158. By default, the <link
  159. linkend="zend.controller.actionhelpers.viewrenderer">ViewRenderer</link>
  160. action helper is enabled. What this means is that by simply
  161. defining an action method and a corresponding view script, you
  162. will immediately get content rendered. By default,
  163. <code>Zend_View</code> is used as the View layer in the MVC. The
  164. <code>ViewRenderer</code> does some magic, and uses the
  165. controller name (e.g., <code>index</code>) and the current
  166. action name (e.g., <code>index</code>) to determine what
  167. template to pull. By default, templates end in the
  168. <code>.phtml</code> extension, so this means that, in the above
  169. example, the template <code>index/index.phtml</code> will be
  170. rendered. Additionally, the <code>ViewRenderer</code>
  171. automatically assumes that the directory <code>views</code> at
  172. the same level as the controller directory will be the base view
  173. directory, and that the actual view scripts will be in the
  174. <code>views/scripts/</code> subdirectory. Thus, the template
  175. rendered will be found in
  176. <code>application/views/scripts/index/index.phtml</code>.
  177. </para>
  178. </sect3>
  179. <sect3 id="zend.controller.quickstart.go.view">
  180. <title>Utwórz własny skrypt widoku</title>
  181. <para>
  182. As mentioned <link
  183. linkend="zend.controller.quickstart.go.controller">in the
  184. previous section</link>, view scripts are found in
  185. <code>application/views/scripts/</code>; the view script for the
  186. default controller and action is in
  187. <code>application/views/scripts/index/index.phtml</code>. Create
  188. this file, and type in some HTML:
  189. </para>
  190. <programlisting role="php"><![CDATA[
  191. <!DOCTYPE html
  192. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  193. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  194. <html>
  195. <head>
  196. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  197. <title>Pierwsza aplikacja Zend Framework</title>
  198. </head>
  199. <body>
  200. <h1>Witaj!</h1>
  201. </body>
  202. </html>
  203. ]]>
  204. </programlisting>
  205. </sect3>
  206. <sect3 id="zend.controller.quickstart.go.errorhandler">
  207. <title>Utwórz kontroler błędu</title>
  208. <para>
  209. Domyślnie, <link
  210. linkend="zend.controller.plugins.standard.errorhandler">wtyczka
  211. obsługi błędów</link> jest zarejestrowana. Ta wtyczka
  212. expects that a controller exists to handle errors.
  213. By default, it assumes an <code>ErrorController</code> in the
  214. default module with an <code>errorAction</code> method:
  215. </para>
  216. <programlisting role="php"><![CDATA[
  217. class ErrorController extends Zend_Controller_Action
  218. {
  219. public function errorAction()
  220. {
  221. }
  222. }
  223. ]]>
  224. </programlisting>
  225. <para>
  226. Assuming the already discussed directory layout, this file will
  227. go in <code>application/controllers/ErrorController.php</code>.
  228. You will also need to create a view script in
  229. <code>application/views/scripts/error/error.phtml</code>; sample
  230. content might look like:
  231. </para>
  232. <programlisting role="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>Błąd</title>
  240. </head>
  241. <body>
  242. <h1>Wystąpił błąd</h1>
  243. <p>Wystąpił błąd; spróbuj ponownie później.</p>
  244. </body>
  245. </html>
  246. ]]>
  247. </programlisting>
  248. </sect3>
  249. <sect3 id="zend.controller.quickstart.go.finish">
  250. <title>Zobacz stronę!</title>
  251. <para>
  252. With your first controller and view under your belt, you can now
  253. fire up your browser and browse to the site. Assuming
  254. <code>example.com</code> is your domain, any of the following
  255. URLs will get to the page we've just created:
  256. </para>
  257. <itemizedlist>
  258. <listitem><para><code>http://example.com/</code></para></listitem>
  259. <listitem><para><code>http://example.com/index</code></para></listitem>
  260. <listitem><para><code>http://example.com/index/index</code></para></listitem>
  261. </itemizedlist>
  262. <para>
  263. Teraz jesteś gotowy do tworzenia kolejnych kontrolerów i metod
  264. akcji. Gratulacje!
  265. </para>
  266. </sect3>
  267. </sect2>
  268. </sect1>