Zend_Application-QuickStart.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 14978 -->
  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. <code>newproject/application/Bootstrap.php</code> und sieht zuerst wie folgt aus:
  70. </para>
  71. <programlisting role="php"><![CDATA[
  72. class Bootstrap extends Zend_Application_Bootstrap_BootstrapAbstract
  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 <code>newproject/public/index.php</code> Datei,
  103. welche <classname>Zend_Application</classname> einfügt und diese ausführt.
  104. </para>
  105. <programlisting role="php"><![CDATA[
  106. <?php
  107. // Define path to application directory
  108. defined('APPLICATION_PATH')
  109. || define('APPLICATION_PATH',
  110. realpath(dirname(__FILE__) . '/../application'));
  111. // Define application environment
  112. defined('APPLICATION_ENV')
  113. || define('APPLICATION_ENV',
  114. (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
  115. : 'production'));
  116. /** Zend_Application */
  117. require_once 'Zend/Application.php';
  118. // Create application, bootstrap, and run
  119. $application = new Zend_Application(
  120. APPLICATION_ENV,
  121. APPLICATION_PATH . '/configs/application.ini'
  122. );
  123. $application->bootstrap();
  124. ->run();
  125. ]]></programlisting>
  126. <para>
  127. Um mit dem Quick Start weiterzumachen, springen Sie bitte <link
  128. linkend="zend.application.quick-start.resources">auf das Ressource Kapitel</link>.
  129. </para>
  130. </sect2>
  131. <sect2 id="zend.application.quick-start.manual">
  132. <title>Zend_Application in der eigenen Anwendung hinzufügen</title>
  133. <para>
  134. Die Basis von <classname>Zend_Application</classname> ist wirklich einfach:
  135. </para>
  136. <itemizedlist>
  137. <listitem><para>
  138. Eine <code>application/Bootstrap.php</code> Datei mit der Klasse
  139. <code>Bootstrap</code> erstellen.
  140. </para></listitem>
  141. <listitem><para>
  142. Eine Konfigurationsdatei <code>application/configs/application.ini</code> mit der
  143. Basiskonfiguration für <classname>Zend_Application</classname> erstellen.
  144. </para></listitem>
  145. <listitem><para>
  146. Ändern von <code>public/index.php</code> um <classname>Zend_Application</classname>
  147. anzupassen.
  148. </para></listitem>
  149. </itemizedlist>
  150. <para>
  151. Zuerst die eigene <code>Bootstrap</code> Klasse erstellen. Erzeuge eine Datei
  152. <code>application/Bootstrap.php</code> mit dem folgenden Inhalt:
  153. </para>
  154. <programlisting role="php"><![CDATA[
  155. <?php
  156. class Bootstrap extends Zend_Application_Bootstrap_BootstrapAbstract
  157. {
  158. }
  159. ]]></programlisting>
  160. <para>
  161. Jetzt die Konfiguration erstellen. Für dieses Tutorial, verwenden wir eine
  162. Konfiguration im INI Stil; man kann natürlich genauso eine XML oder PHP
  163. Konfigurationsdatei verwenden. Erstelle eine Datei
  164. <code>application/configs/application.ini</code>, und füge den folgenden Inhalt ein:
  165. </para>
  166. <programlisting role="dosini"><![CDATA[
  167. [production]
  168. phpSettings.display_startup_errors = 0
  169. phpSettings.display_errors = 0
  170. includePaths.library = APPLICATION_PATH "/../library"
  171. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  172. bootstrap.class = "Bootstrap"
  173. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  174. [staging : production]
  175. [testing : production]
  176. phpSettings.display_startup_errors = 1
  177. phpSettings.display_errors = 1
  178. [development : production]
  179. phpSettings.display_startup_errors = 1
  180. phpSettings.display_errors = 1
  181. ]]></programlisting>
  182. <para>
  183. Jetz verändern wir das Gateway Skript <code>public/index.php</code>. Wenn die Datei
  184. nicht existiert erstellen wir Sie; andernfalls ersetzen wir Sie mit dem folgenden
  185. Inhalt:
  186. </para>
  187. <programlisting role="php"><![CDATA[
  188. <?php
  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. /** Zend_Application */
  199. require_once 'Zend/Application.php';
  200. // Create application, bootstrap, and run
  201. $application = new Zend_Application(
  202. APPLICATION_ENV,
  203. APPLICATION_PATH . '/configs/application.ini'
  204. );
  205. $application->bootstrap()
  206. ->run();
  207. ]]></programlisting>
  208. <para>
  209. Es ist zu beachten das die Environment Konstante der Anwendung nach einer Environment
  210. Variable "APPLICATION_ENV" sucht. Wir empfehlen diese im Web Server Environment zu
  211. setzen. In Apache kann man diese entweder in der vhost Definition setzen, oder in der
  212. <code>.htaccess</code> Datei. Wir empfehlen den folgenden Inhalt in der Datei
  213. <code>public/.htacces</code>:
  214. </para>
  215. <programlisting role="conf"><![CDATA[
  216. SetEnv APPLICATION_ENV development
  217. RewriteEngine On
  218. RewriteCond %{REQUEST_FILENAME} -s [OR]
  219. RewriteCond %{REQUEST_FILENAME} -l [OR]
  220. RewriteCond %{REQUEST_FILENAME} -d
  221. RewriteRule ^.*$ - [NC,L]
  222. RewriteRule ^.*$ index.php [NC,L]
  223. ]]></programlisting>
  224. <para>
  225. An diesem Punkt, wurde alles gesetzt um die Vorteile von <classname>Zend_Application</classname>
  226. zu verwenden.
  227. </para>
  228. </sect2>
  229. <sect2 id="zend.application.quick-start.resources">
  230. <title>Hinzufügen und Erstellen von Ressourcen</title>
  231. <para>
  232. Wenn man den Anleitungen von oben gefolgt ist, dann verwendet die Bootstrap Klasse
  233. einen Front Controller, und wenn Sie gestartet wird, wird Sie den Front Controller
  234. ausführen. Trotzdem wird man, in allen Fällen, etwas mehr Konfiguration als das
  235. benötigen.
  236. </para>
  237. <para>
  238. In diesem Kapitel werden wir zwei Ressourcen zur anwendung hinzufügen. Zuerst werden
  239. wir Layouts erstellen, und dann werden wir ein View Objekt anpassen.
  240. </para>
  241. <para>
  242. Eine der von <classname>Zend_Application</classname> angebotenen Standardressourcen ist die
  243. "layout" Ressource. Diese Ressource erwartet, das man Konfgurationswerte definiert,
  244. welche dann verwendet werden um die <classname>Zend_Layout</classname> Instanz zu konfigurieren.
  245. </para>
  246. <para>
  247. Um Sie zu verwenden müssen wir die Konfigurationsdatei aktualisieren.
  248. </para>
  249. <programlisting role="dosini"><![CDATA[
  250. [production]
  251. phpSettings.display_startup_errors = 0
  252. phpSettings.display_errors = 0
  253. includePaths.library = APPLICATION_PATH "/../library"
  254. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  255. bootstrap.class = "Bootstrap"
  256. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  257. ; ADD THE FOLLOWING LINES
  258. resources.layout.layout = "layout.phtml"
  259. resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
  260. [staging : production]
  261. [testing : production]
  262. phpSettings.display_startup_errors = 1
  263. phpSettings.display_errors = 1
  264. [development : production]
  265. phpSettings.display_startup_errors = 1
  266. phpSettings.display_errors = 1
  267. ]]></programlisting>
  268. <para>
  269. Wenn man es nicht bereits getan hat, muß man das Verzeichnis
  270. <code>application/layouts/scripts/</code> und die Datei <code>layout.phtml</code> in
  271. diesem Verzeichnis erstellen. Ein gutes Layout zum Starten ist das folgende (und ist
  272. mit der View Ressource die als nächstes besprochen wird verbunden):
  273. </para>
  274. <programlisting role="php"><![CDATA[
  275. <?php echo $this->doctype() ?>
  276. <html>
  277. <head>
  278. <?php echo $this->headTitle() ?>
  279. <?php echo $this->headLink() ?>
  280. <?php echo $this->headStyle() ?>
  281. <?php echo $this->headScript() ?>
  282. </head>
  283. <body>
  284. <?php echo $this->layout()->content ?>
  285. </body>
  286. </html>
  287. ]]></programlisting>
  288. <para>
  289. An diesem Punkt, hat man ein funktionierendes Layout.
  290. </para>
  291. <para>
  292. Jetzt fügen wir eine eigene View Ressource hinzu. Wenn die View initialisiert wird,
  293. will man den HTML DocType und einen Standardwert für den im HTML Kopf zu verwendenden
  294. Titel setzen. Das kann durch die Änderung der <code>Bootstrap</code> Klasse und dem
  295. hinzufügen einer Methode gemacht werden:
  296. </para>
  297. <programlisting role="php"><![CDATA[
  298. <?php
  299. class Bootstrap extends Zend_Application_Bootstrap_BootstrapAbstract
  300. {
  301. protected function _initView()
  302. {
  303. // Initialize view
  304. $view = new Zend_View();
  305. $view->doctype('XHTML1_STRICT');
  306. $view->headTitle('My First Zend Framework Application');
  307. // Add it to the ViewRenderer
  308. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
  309. 'ViewRenderer'
  310. );
  311. $viewRenderer->setView($view);
  312. // Return it, so that it can be stored by the bootstrap
  313. return $view;
  314. }
  315. }
  316. ]]></programlisting>
  317. <para>
  318. Diese Methode wird automatisch ausgeführt wenn das Bootstrap der Anwendung ausgeführt
  319. wird und stellt sicher das die View so initialisiert wird wie die Anwendung das
  320. benötigt.
  321. </para>
  322. </sect2>
  323. <sect2 id="zend.application.quick-start.next-steps">
  324. <title>Nächste Schritte mit Zend_Application</title>
  325. <para>
  326. Das oben erwähnte reicht um mit <classname>Zend_Application</classname> zu beginnen und das
  327. Bootstrap der eigenen Anwendung zu erstellen. Von hier an, sollte man beginnen
  328. Ressource-Methoden zu erstellen, oder, für maximale Wiederverwendbarkeit,
  329. Ressource-Plugin Klassen. Lesen Sie weiter, um mehr zu lernen!
  330. </para>
  331. </sect2>
  332. </sect1>