2
0

Zend_Application-QuickStart.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 20763 -->
  3. <!-- Reviewed: 20763 -->
  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 entsprechenden
  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 unten 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 Kommando <command>zf</command> (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 Kommando <filename>zf.bat</filename>:
  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. Sie werden auch bemerken, dass 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> aufruft 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>
  139. <para>
  140. Eine Datei <filename>application/Bootstrap.php</filename> mit der Klasse
  141. <classname>Bootstrap</classname> erstellen.
  142. </para>
  143. </listitem>
  144. <listitem>
  145. <para>
  146. Eine Konfigurationsdatei
  147. <filename>application/configs/application.ini</filename> mit der
  148. Basiskonfiguration für <classname>Zend_Application</classname> erstellen.
  149. </para>
  150. </listitem>
  151. <listitem>
  152. <para>
  153. Ändern von <filename>public/index.php</filename> um
  154. <classname>Zend_Application</classname> anzupassen.
  155. </para>
  156. </listitem>
  157. </itemizedlist>
  158. <para>
  159. Zuerst die eigene <classname>Bootstrap</classname>-Klasse erstellen. Erzeuge eine Datei
  160. <filename>application/Bootstrap.php</filename> mit dem folgenden Inhalt:
  161. </para>
  162. <programlisting language="php"><![CDATA[
  163. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  164. {
  165. }
  166. ]]></programlisting>
  167. <para>
  168. Jetzt die Konfiguration erstellen. Für dieses Tutorial, verwenden wir eine
  169. Konfiguration im <acronym>INI</acronym>-Stil; man kann natürlich genauso eine
  170. <acronym>XML</acronym>- oder <acronym>PHP</acronym>-Konfigurationsdatei verwenden.
  171. Erstelle eine Datei <filename>application/configs/application.ini</filename>, und füge
  172. den folgenden Inhalt ein:
  173. </para>
  174. <programlisting language="dosini"><![CDATA[
  175. [production]
  176. phpSettings.display_startup_errors = 0
  177. phpSettings.display_errors = 0
  178. includePaths.library = APPLICATION_PATH "/../library"
  179. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  180. bootstrap.class = "Bootstrap"
  181. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  182. [staging : production]
  183. [testing : production]
  184. phpSettings.display_startup_errors = 1
  185. phpSettings.display_errors = 1
  186. [development : production]
  187. phpSettings.display_startup_errors = 1
  188. phpSettings.display_errors = 1
  189. ]]></programlisting>
  190. <para>
  191. Jetz verändern wir das Gateway Skript <filename>public/index.php</filename>. Wenn die
  192. Datei nicht existiert, erstellen wir sie; andernfalls ersetzen wir sie mit dem folgenden
  193. Inhalt:
  194. </para>
  195. <programlisting language="php"><![CDATA[
  196. // Define path to application directory
  197. defined('APPLICATION_PATH')
  198. || define('APPLICATION_PATH',
  199. realpath(dirname(__FILE__) . '/../application'));
  200. // Define application environment
  201. defined('APPLICATION_ENV')
  202. || define('APPLICATION_ENV',
  203. (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
  204. : 'production'));
  205. // Typischerweise wird man das eigene library/ Verzeichnis zum include_path
  206. // hinzufügen wollen, speziell wenn es die ZF Installation enthält
  207. set_include_path(implode(PATH_SEPARATOR, array(
  208. dirname(dirname(__FILE__)) . '/library',
  209. get_include_path(),
  210. )));
  211. /** Zend_Application */
  212. require_once 'Zend/Application.php';
  213. // Create application, bootstrap, and run
  214. $application = new Zend_Application(
  215. APPLICATION_ENV,
  216. APPLICATION_PATH . '/configs/application.ini'
  217. );
  218. $application->bootstrap()
  219. ->run();
  220. ]]></programlisting>
  221. <para>
  222. Es ist zu beachten, dass die Umgebungskonstante der Anwendung nach einer
  223. Umgebungsvariablen "APPLICATION_ENV" sucht. Wir empfehlen diese im Web Server Environment
  224. zu setzen. In Apache kann man diese entweder in der vhost Definition setzen, oder in der
  225. Datei <filename>.htaccess</filename>. Wir empfehlen den folgenden Inhalt in der Datei
  226. <filename>public/.htaccess</filename>:
  227. </para>
  228. <programlisting language="conf"><![CDATA[
  229. SetEnv APPLICATION_ENV development
  230. RewriteEngine On
  231. RewriteCond %{REQUEST_FILENAME} -s [OR]
  232. RewriteCond %{REQUEST_FILENAME} -l [OR]
  233. RewriteCond %{REQUEST_FILENAME} -d
  234. RewriteRule ^.*$ - [NC,L]
  235. RewriteRule ^.*$ index.php [NC,L]
  236. ]]></programlisting>
  237. <note>
  238. <title>Mehr über mod_rewrite lernen</title>
  239. <para>
  240. Die obigen Rewrite-Regeln erlauben es, auf jede Datei im DocumentRoot des eigenen
  241. virtuellen Hosts zuzugreifen. Wenn es Dateien gibt, die man auf diesem Weg nicht
  242. bereitstellen will, muss man in seinen Regeln restriktiver sein. Gehe zur
  243. Apache WebSite und <ulink
  244. url="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html">lerne mehr über
  245. mod_rewrite</ulink>.
  246. </para>
  247. </note>
  248. <para>
  249. An diesem Punkt haben wir es geschafft und können damit beginnen, die Vorteile von
  250. <classname>Zend_Application</classname> zu nutzen.
  251. </para>
  252. </sect2>
  253. <sect2 id="zend.application.quick-start.resources">
  254. <title>Hinzufügen und Erstellen von Ressourcen</title>
  255. <para>
  256. Wenn man den Anleitungen von oben gefolgt ist, dann verwendet die Bootstrap-Klasse
  257. einen FrontController, und wenn sie gestartet wird, wird sie den FrontController
  258. ausführen. Trotzdem wird man mit großer Wahrscheinlichkeit etwas mehr Konfiguration
  259. als das benötigen.
  260. </para>
  261. <para>
  262. In diesem Kapitel werden wir zwei Ressourcen zur Anwendung hinzufügen. Zuerst werden
  263. wir Layouts erstellen, und dann werden wir das View Objekt anpassen.
  264. </para>
  265. <para>
  266. Eine der von <classname>Zend_Application</classname> angebotenen Standardressourcen ist
  267. die "layout" Ressource. Diese Ressource erwartet, dass man Konfigurationswerte definiert,
  268. welche dann verwendet werden, um unsere Instanz von <classname>Zend_Layout</classname>
  269. zu konfigurieren.
  270. </para>
  271. <para>
  272. Um sie zu verwenden, müssen wir die Konfigurationsdatei aktualisieren.
  273. </para>
  274. <programlisting language="dosini"><![CDATA[
  275. [production]
  276. phpSettings.display_startup_errors = 0
  277. phpSettings.display_errors = 0
  278. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  279. bootstrap.class = "Bootstrap"
  280. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  281. ; ADD THE FOLLOWING LINES
  282. resources.layout.layout = "layout"
  283. resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
  284. [staging : production]
  285. [testing : production]
  286. phpSettings.display_startup_errors = 1
  287. phpSettings.display_errors = 1
  288. [development : production]
  289. phpSettings.display_startup_errors = 1
  290. phpSettings.display_errors = 1
  291. ]]></programlisting>
  292. <para>
  293. Wenn man es nicht bereits getan hat, muß man das Verzeichnis
  294. <filename>application/layouts/scripts/</filename> und die Datei
  295. <filename>layout.phtml</filename> in diesem Verzeichnis erstellen. Ein gutes Layout zum
  296. Starten ist das folgende (und ist mit der View Ressource verbunden, die als nächstes
  297. besprochen wird):
  298. </para>
  299. <programlisting language="php"><![CDATA[
  300. <?php echo $this->doctype() ?>
  301. <html>
  302. <head>
  303. <?php echo $this->headTitle() ?>
  304. <?php echo $this->headLink() ?>
  305. <?php echo $this->headStyle() ?>
  306. <?php echo $this->headScript() ?>
  307. </head>
  308. <body>
  309. <?php echo $this->layout()->content ?>
  310. </body>
  311. </html>
  312. ]]></programlisting>
  313. <para>
  314. An diesem Punkt haben wir ein funktionierendes Layout.
  315. </para>
  316. <para>
  317. Jetzt fügen wir eine eigene View Ressource hinzu. Wenn die View initialisiert wird,
  318. will man den <acronym>HTML</acronym> DocType und einen Standardwert für den im
  319. <acronym>HTML</acronym>-Kopf zu verwendenden Titel setzen. Das kann durch die Änderung
  320. der <classname>Bootstrap</classname>-Klasse und dem Hinzufügen einer Methode gemacht
  321. werden:
  322. </para>
  323. <programlisting language="php"><![CDATA[
  324. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  325. {
  326. protected function _initView()
  327. {
  328. // View initialisieren
  329. $view = new Zend_View();
  330. $view->doctype('XHTML1_STRICT');
  331. $view->headTitle('My First Zend Framework Application');
  332. // View zum ViewRenderer hinzufügen
  333. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
  334. 'ViewRenderer'
  335. );
  336. $viewRenderer->setView($view);
  337. // Zurückgeben, damit sie von Bootstrap gespeichert werden kann
  338. return $view;
  339. }
  340. }
  341. ]]></programlisting>
  342. <para>
  343. Diese Methode wird automatisch ausgeführt, wenn das Bootstrap der Anwendung ausgeführt
  344. wird und stellt sicher, dass die View so initialisiert wird, wie die Anwendung das
  345. benötigt.
  346. </para>
  347. </sect2>
  348. <sect2 id="zend.application.quick-start.next-steps">
  349. <title>Nächste Schritte mit Zend_Application</title>
  350. <para>
  351. Das oben erwähnte reicht, um mit <classname>Zend_Application</classname> zu beginnen und
  352. das Bootstrap der eigenen Anwendung zu erstellen. Von hier an sollte man beginnen
  353. Ressource-Methoden zu erstellen, oder für maximale Wiederverwendbarkeit,
  354. Ressource-Plugin Klassen. Lesen Sie weiter, um mehr zu lernen!
  355. </para>
  356. </sect2>
  357. </sect1>