Zend_Application-QuickStart.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 17372 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="zend.application.quick-start">
  5. <title>Zend_Application Quick Start</title>
  6. <para>
  7. Es gibt zwei Wege um mit <classname>Zend_Application</classname> anzufangen, und diese
  8. hängen davon ab wie man das Projekt beginnt. In jedem Fall beginnt man immer mit der
  9. Erstellung einer <classname>Bootstrap</classname> Klasse und einer diesbezüglichen
  10. Konfigurationsdatei.
  11. </para>
  12. <para>
  13. Wenn man plant <classname>Zend_Tool</classname> zu verwenden um das eigene Projekt zu
  14. erstellen sollte man anbei weiterlesen. Wenn man <classname>Zend_Application</classname> zu
  15. einem existierenden Projekt hinzufügen will, sollte man <link
  16. linkend="zend.application.quick-start.manual"> hier weiterlesen</link>.
  17. </para>
  18. <sect2 id="zend.application.quick-start.zend-tool">
  19. <title>Verwenden von Zend_Tool</title>
  20. <para>
  21. Der schnellste Weg um mit <classname>Zend_Application</classname> zu beginnen ist die
  22. Verwendung von <classname>Zend_Tool</classname> um das Projekt zu erstellen. Das
  23. erstellt auch die <classname>Bootstrap</classname> Klasse und die Datei.
  24. </para>
  25. <para>
  26. Um ein Projekt zu erstellen muß einfach das <command>zf</command> Kommando (auf *nix
  27. Systemen) ausgeführt werden:
  28. </para>
  29. <programlisting language="sh"><![CDATA[
  30. % zf create project newproject
  31. ]]></programlisting>
  32. <para>
  33. Oder unter Windows das <filename>zf.bat</filename> Kommando:
  34. </para>
  35. <programlisting language="dos"><![CDATA[
  36. C:> zf.bat create project newproject
  37. ]]></programlisting>
  38. <para>
  39. Beides erstellt eine Projektstruktur die wie folgt aussieht:
  40. </para>
  41. <programlisting language="text"><![CDATA[
  42. newproject
  43. |-- application
  44. | |-- Bootstrap.php
  45. | |-- configs
  46. | | `-- application.ini
  47. | |-- controllers
  48. | | |-- ErrorController.php
  49. | | `-- IndexController.php
  50. | |-- models
  51. | `-- views
  52. | |-- helpers
  53. | `-- scripts
  54. | |-- error
  55. | | `-- error.phtml
  56. | `-- index
  57. | `-- index.phtml
  58. |-- library
  59. |-- public
  60. | `-- index.php
  61. `-- tests
  62. |-- application
  63. | `-- bootstrap.php
  64. |-- library
  65. | `-- bootstrap.php
  66. `-- phpunit.xml
  67. ]]></programlisting>
  68. <para>
  69. Im obigen Diagramm ist die Bootstrap unter
  70. <filename>newproject/application/Bootstrap.php</filename> und sieht zuerst wie folgt
  71. aus:
  72. </para>
  73. <programlisting language="php"><![CDATA[
  74. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  75. {
  76. }
  77. ]]></programlisting>
  78. <para>
  79. Es ist auch zu bemerken das eine Konfigurationdatei unter
  80. <filename>newproject/application/configs/application.ini</filename> erstellt wurde.
  81. Diese hat den folgenden Inhalt:
  82. </para>
  83. <programlisting language="dosini"><![CDATA[
  84. [production]
  85. phpSettings.display_startup_errors = 0
  86. phpSettings.display_errors = 0
  87. includePaths.library = APPLICATION_PATH "/../library"
  88. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  89. bootstrap.class = "Bootstrap"
  90. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  91. [staging : production]
  92. [testing : production]
  93. phpSettings.display_startup_errors = 1
  94. phpSettings.display_errors = 1
  95. [development : production]
  96. phpSettings.display_startup_errors = 1
  97. phpSettings.display_errors = 1
  98. ]]></programlisting>
  99. <para>
  100. Alle Einstellungen in dieser Konfigurationsdatei sind für die Verwendung mit
  101. <classname>Zend_Application</classname> und der Bootstrap.
  102. </para>
  103. <para>
  104. Eine andere Datei von Interesse ist die <filename>newproject/public/index.php</filename>
  105. Datei, welche <classname>Zend_Application</classname> einfügt und diese ausführt.
  106. </para>
  107. <programlisting language="php"><![CDATA[
  108. // Define path to application directory
  109. defined('APPLICATION_PATH')
  110. || define('APPLICATION_PATH',
  111. realpath(dirname(__FILE__) . '/../application'));
  112. // Define application environment
  113. defined('APPLICATION_ENV')
  114. || define('APPLICATION_ENV',
  115. (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
  116. : 'production'));
  117. /** Zend_Application */
  118. require_once 'Zend/Application.php';
  119. // Create application, bootstrap, and run
  120. $application = new Zend_Application(
  121. APPLICATION_ENV,
  122. APPLICATION_PATH . '/configs/application.ini'
  123. );
  124. $application->bootstrap()
  125. ->run();
  126. ]]></programlisting>
  127. <para>
  128. Um mit dem Quick Start weiterzumachen, springen Sie bitte <link
  129. linkend="zend.application.quick-start.resources">auf das Ressource Kapitel</link>.
  130. </para>
  131. </sect2>
  132. <sect2 id="zend.application.quick-start.manual">
  133. <title>Zend_Application in der eigenen Anwendung hinzufügen</title>
  134. <para>
  135. Die Basis von <classname>Zend_Application</classname> ist wirklich einfach:
  136. </para>
  137. <itemizedlist>
  138. <listitem><para>
  139. Eine <filename>application/Bootstrap.php</filename> Datei mit der Klasse
  140. <classname>Bootstrap</classname> erstellen.
  141. </para></listitem>
  142. <listitem><para>
  143. Eine Konfigurationsdatei <filename>application/configs/application.ini</filename>
  144. mit der Basiskonfiguration für <classname>Zend_Application</classname> erstellen.
  145. </para></listitem>
  146. <listitem><para>
  147. Ändern von <filename>public/index.php</filename> um
  148. <classname>Zend_Application</classname> anzupassen.
  149. </para></listitem>
  150. </itemizedlist>
  151. <para>
  152. Zuerst die eigene <classname>Bootstrap</classname> Klasse erstellen. Erzeuge eine Datei
  153. <filename>application/Bootstrap.php</filename> mit dem folgenden Inhalt:
  154. </para>
  155. <programlisting language="php"><![CDATA[
  156. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  157. {
  158. }
  159. ]]></programlisting>
  160. <para>
  161. Jetzt die Konfiguration erstellen. Für dieses Tutorial, verwenden wir eine
  162. Konfiguration im <acronym>INI</acronym> Stil; man kann natürlich genauso eine
  163. <acronym>XML</acronym> oder <acronym>PHP</acronym> Konfigurationsdatei verwenden.
  164. Erstelle eine Datei <filename>application/configs/application.ini</filename>, und füge
  165. den folgenden Inhalt ein:
  166. </para>
  167. <programlisting language="dosini"><![CDATA[
  168. [production]
  169. phpSettings.display_startup_errors = 0
  170. phpSettings.display_errors = 0
  171. includePaths.library = APPLICATION_PATH "/../library"
  172. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  173. bootstrap.class = "Bootstrap"
  174. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  175. [staging : production]
  176. [testing : production]
  177. phpSettings.display_startup_errors = 1
  178. phpSettings.display_errors = 1
  179. [development : production]
  180. phpSettings.display_startup_errors = 1
  181. phpSettings.display_errors = 1
  182. ]]></programlisting>
  183. <para>
  184. Jetz verändern wir das Gateway Skript <filename>public/index.php</filename>. Wenn die
  185. Datei nicht existiert erstellen wir Sie; andernfalls ersetzen wir Sie mit dem folgenden
  186. Inhalt:
  187. </para>
  188. <programlisting language="php"><![CDATA[
  189. // Define path to application directory
  190. defined('APPLICATION_PATH')
  191. || define('APPLICATION_PATH',
  192. realpath(dirname(__FILE__) . '/../application'));
  193. // Define application environment
  194. defined('APPLICATION_ENV')
  195. || define('APPLICATION_ENV',
  196. (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
  197. : 'production'));
  198. // Typischerweise wird man das eigene library/ Verzeichnis zum include_path
  199. // hinzufügen wollen, speziell wenn es die ZF Installation enthält
  200. set_include_path(implode(PATH_SEPARATOR, array(
  201. dirname(dirname(__FILE__)) . '/library',
  202. get_include_path(),
  203. )));
  204. /** Zend_Application */
  205. require_once 'Zend/Application.php';
  206. // Create application, bootstrap, and run
  207. $application = new Zend_Application(
  208. APPLICATION_ENV,
  209. APPLICATION_PATH . '/configs/application.ini'
  210. );
  211. $application->bootstrap()
  212. ->run();
  213. ]]></programlisting>
  214. <para>
  215. Es ist zu beachten das die Environment Konstante der Anwendung nach einer Environment
  216. Variable "APPLICATION_ENV" sucht. Wir empfehlen diese im Web Server Environment zu
  217. setzen. In Apache kann man diese entweder in der vhost Definition setzen, oder in der
  218. <filename>.htaccess</filename> Datei. Wir empfehlen den folgenden Inhalt in der Datei
  219. <filename>public/.htaccess</filename>:
  220. </para>
  221. <programlisting language="conf"><![CDATA[
  222. SetEnv APPLICATION_ENV development
  223. RewriteEngine On
  224. RewriteCond %{REQUEST_FILENAME} -s [OR]
  225. RewriteCond %{REQUEST_FILENAME} -l [OR]
  226. RewriteCond %{REQUEST_FILENAME} -d
  227. RewriteRule ^.*$ - [NC,L]
  228. RewriteRule ^.*$ index.php [NC,L]
  229. ]]></programlisting>
  230. <note>
  231. <title>Mehr über mod_rewrite lernen</title>
  232. <para>
  233. Die obigen Rewrite Regeln erlauben es auf jede Datei im Document Root des eigenen
  234. virtuellen Host's zuzugreifen. Wenn es Dateien gibt die man auf diesem Weg nicht
  235. bereitstellen will, muss man in seinen Regeln restriktiver sein. Gehe zur
  236. Apache WebSite und <ulink
  237. url="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html">lerne mehr über
  238. mod_rewrite</ulink>.
  239. </para>
  240. </note>
  241. <para>
  242. An diesem Punkt, wurde alles gesetzt um die Vorteile von
  243. <classname>Zend_Application</classname> zu verwenden.
  244. </para>
  245. </sect2>
  246. <sect2 id="zend.application.quick-start.resources">
  247. <title>Hinzufügen und Erstellen von Ressourcen</title>
  248. <para>
  249. Wenn man den Anleitungen von oben gefolgt ist, dann verwendet die Bootstrap Klasse
  250. einen Front Controller, und wenn Sie gestartet wird, wird Sie den Front Controller
  251. ausführen. Trotzdem wird man, in allen Fällen, etwas mehr Konfiguration als das
  252. benötigen.
  253. </para>
  254. <para>
  255. In diesem Kapitel werden wir zwei Ressourcen zur anwendung hinzufügen. Zuerst werden
  256. wir Layouts erstellen, und dann werden wir ein View Objekt anpassen.
  257. </para>
  258. <para>
  259. Eine der von <classname>Zend_Application</classname> angebotenen Standardressourcen ist
  260. die "layout" Ressource. Diese Ressource erwartet, das man Konfgurationswerte definiert,
  261. welche dann verwendet werden um die <classname>Zend_Layout</classname> Instanz zu
  262. konfigurieren.
  263. </para>
  264. <para>
  265. Um Sie zu verwenden müssen wir die Konfigurationsdatei aktualisieren.
  266. </para>
  267. <programlisting language="dosini"><![CDATA[
  268. [production]
  269. phpSettings.display_startup_errors = 0
  270. phpSettings.display_errors = 0
  271. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  272. bootstrap.class = "Bootstrap"
  273. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  274. ; ADD THE FOLLOWING LINES
  275. resources.layout.layout = "layout"
  276. resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
  277. [staging : production]
  278. [testing : production]
  279. phpSettings.display_startup_errors = 1
  280. phpSettings.display_errors = 1
  281. [development : production]
  282. phpSettings.display_startup_errors = 1
  283. phpSettings.display_errors = 1
  284. ]]></programlisting>
  285. <para>
  286. Wenn man es nicht bereits getan hat, muß man das Verzeichnis
  287. <filename>application/layouts/scripts/</filename> und die Datei
  288. <filename>layout.phtml</filename> in diesem Verzeichnis erstellen. Ein gutes Layout zum
  289. Starten ist das folgende (und ist mit der View Ressource die als nächstes besprochen
  290. wird verbunden):
  291. </para>
  292. <programlisting language="php"><![CDATA[
  293. <?php echo $this->doctype() ?>
  294. <html>
  295. <head>
  296. <?php echo $this->headTitle() ?>
  297. <?php echo $this->headLink() ?>
  298. <?php echo $this->headStyle() ?>
  299. <?php echo $this->headScript() ?>
  300. </head>
  301. <body>
  302. <?php echo $this->layout()->content ?>
  303. </body>
  304. </html>
  305. ]]></programlisting>
  306. <para>
  307. An diesem Punkt, hat man ein funktionierendes Layout.
  308. </para>
  309. <para>
  310. Jetzt fügen wir eine eigene View Ressource hinzu. Wenn die View initialisiert wird,
  311. will man den <acronym>HTML</acronym> DocType und einen Standardwert für den im
  312. <acronym>HTML</acronym> Kopf zu verwendenden Titel setzen. Das kann durch die Änderung
  313. der <classname>Bootstrap</classname> Klasse und dem Hinzufügen einer Methode gemacht
  314. werden:
  315. </para>
  316. <programlisting language="php"><![CDATA[
  317. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  318. {
  319. protected function _initView()
  320. {
  321. // Initialize view
  322. $view = new Zend_View();
  323. $view->doctype('XHTML1_STRICT');
  324. $view->headTitle('My First Zend Framework Application');
  325. // Add it to the ViewRenderer
  326. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
  327. 'ViewRenderer'
  328. );
  329. $viewRenderer->setView($view);
  330. // Return it, so that it can be stored by the bootstrap
  331. return $view;
  332. }
  333. }
  334. ]]></programlisting>
  335. <para>
  336. Diese Methode wird automatisch ausgeführt wenn das Bootstrap der Anwendung ausgeführt
  337. wird und stellt sicher das die View so initialisiert wird wie die Anwendung das
  338. benötigt.
  339. </para>
  340. </sect2>
  341. <sect2 id="zend.application.quick-start.next-steps">
  342. <title>Nächste Schritte mit Zend_Application</title>
  343. <para>
  344. Das oben erwähnte reicht um mit <classname>Zend_Application</classname> zu beginnen und
  345. das Bootstrap der eigenen Anwendung zu erstellen. Von hier an, sollte man beginnen
  346. Ressource-Methoden zu erstellen, oder, für maximale Wiederverwendbarkeit,
  347. Ressource-Plugin Klassen. Lesen Sie weiter, um mehr zu lernen!
  348. </para>
  349. </sect2>
  350. </sect1>