Zend_Application-QuickStart.xml 13 KB

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