Zend_Application-QuickStart.xml 14 KB

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