| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- EN-Revision: 20763 -->
- <!-- Reviewed: 20763 -->
- <sect1 id="zend.application.quick-start">
- <title>Zend_Application Quick Start</title>
- <para>
- Es gibt zwei Wege um mit <classname>Zend_Application</classname> anzufangen, und diese
- hängen davon ab, wie man das Projekt beginnt. In jedem Fall beginnt man immer mit der
- Erstellung einer <classname>Bootstrap</classname>-Klasse und einer entsprechenden
- Konfigurationsdatei.
- </para>
- <para>
- Wenn man plant <classname>Zend_Tool</classname> zu verwenden, um das eigene Projekt zu
- erstellen, sollte man unten weiterlesen. Wenn man <classname>Zend_Application</classname> zu
- einem existierenden Projekt hinzufügen will, sollte man <link
- linkend="zend.application.quick-start.manual">hier weiterlesen</link>.
- </para>
- <sect2 id="zend.application.quick-start.zend-tool">
- <title>Verwenden von Zend_Tool</title>
- <para>
- Der schnellste Weg, um mit <classname>Zend_Application</classname> zu beginnen, ist die
- Verwendung von <classname>Zend_Tool</classname> um das Projekt zu erstellen. Das
- erstellt auch die <classname>Bootstrap</classname>-Klasse und die Datei.
- </para>
- <para>
- Um ein Projekt zu erstellen, muß einfach das Kommando <command>zf</command> (auf *nix
- Systemen) ausgeführt werden:
- </para>
- <programlisting language="sh"><![CDATA[
- % zf create project newproject
- ]]></programlisting>
- <para>
- Oder unter Windows das Kommando <filename>zf.bat</filename>:
- </para>
- <programlisting language="dos"><![CDATA[
- C:> zf.bat create project newproject
- ]]></programlisting>
- <para>
- Beides erstellt eine Projektstruktur, die wie folgt aussieht:
- </para>
- <programlisting language="text"><![CDATA[
- newproject
- |-- application
- | |-- Bootstrap.php
- | |-- configs
- | | `-- application.ini
- | |-- controllers
- | | |-- ErrorController.php
- | | `-- IndexController.php
- | |-- models
- | `-- views
- | |-- helpers
- | `-- scripts
- | |-- error
- | | `-- error.phtml
- | `-- index
- | `-- index.phtml
- |-- library
- |-- public
- | `-- index.php
- `-- tests
- |-- application
- | `-- bootstrap.php
- |-- library
- | `-- bootstrap.php
- `-- phpunit.xml
- ]]></programlisting>
- <para>
- Im obigen Diagramm ist die Bootstrap unter
- <filename>newproject/application/Bootstrap.php</filename> und sieht zuerst wie folgt
- aus:
- </para>
- <programlisting language="php"><![CDATA[
- class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
- {
- }
- ]]></programlisting>
- <para>
- Sie werden auch bemerken, dass eine Konfigurationdatei unter
- <filename>newproject/application/configs/application.ini</filename> erstellt wurde.
- Diese hat den folgenden Inhalt:
- </para>
- <programlisting language="dosini"><![CDATA[
- [production]
- phpSettings.display_startup_errors = 0
- phpSettings.display_errors = 0
- includePaths.library = APPLICATION_PATH "/../library"
- bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
- bootstrap.class = "Bootstrap"
- resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
- [staging : production]
- [testing : production]
- phpSettings.display_startup_errors = 1
- phpSettings.display_errors = 1
- [development : production]
- phpSettings.display_startup_errors = 1
- phpSettings.display_errors = 1
- ]]></programlisting>
- <para>
- Alle Einstellungen in dieser Konfigurationsdatei sind für die Verwendung mit
- <classname>Zend_Application</classname> und der Bootstrap.
- </para>
- <para>
- Eine andere Datei von Interesse ist die <filename>newproject/public/index.php</filename>
- Datei, welche <classname>Zend_Application</classname> aufruft und diese ausführt.
- </para>
- <programlisting language="php"><![CDATA[
- // Define path to application directory
- defined('APPLICATION_PATH')
- || define('APPLICATION_PATH',
- realpath(dirname(__FILE__) . '/../application'));
- // Define application environment
- defined('APPLICATION_ENV')
- || define('APPLICATION_ENV',
- (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
- : 'production'));
- /** Zend_Application */
- require_once 'Zend/Application.php';
- // Create application, bootstrap, and run
- $application = new Zend_Application(
- APPLICATION_ENV,
- APPLICATION_PATH . '/configs/application.ini'
- );
- $application->bootstrap()
- ->run();
- ]]></programlisting>
- <para>
- Um mit dem Quick Start weiterzumachen, springen Sie bitte <link
- linkend="zend.application.quick-start.resources">auf das Ressource-Kapitel</link>.
- </para>
- </sect2>
- <sect2 id="zend.application.quick-start.manual">
- <title>Zend_Application in der eigenen Anwendung hinzufügen</title>
- <para>
- Die Basis von <classname>Zend_Application</classname> ist wirklich einfach:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- Eine Datei <filename>application/Bootstrap.php</filename> mit der Klasse
- <classname>Bootstrap</classname> erstellen.
- </para>
- </listitem>
- <listitem>
- <para>
- Eine Konfigurationsdatei
- <filename>application/configs/application.ini</filename> mit der
- Basiskonfiguration für <classname>Zend_Application</classname> erstellen.
- </para>
- </listitem>
- <listitem>
- <para>
- Ändern von <filename>public/index.php</filename> um
- <classname>Zend_Application</classname> anzupassen.
- </para>
- </listitem>
- </itemizedlist>
- <para>
- Zuerst die eigene <classname>Bootstrap</classname>-Klasse erstellen. Erzeuge eine Datei
- <filename>application/Bootstrap.php</filename> mit dem folgenden Inhalt:
- </para>
- <programlisting language="php"><![CDATA[
- class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
- {
- }
- ]]></programlisting>
- <para>
- Jetzt die Konfiguration erstellen. Für dieses Tutorial, verwenden wir eine
- Konfiguration im <acronym>INI</acronym>-Stil; man kann natürlich genauso eine
- <acronym>XML</acronym>- oder <acronym>PHP</acronym>-Konfigurationsdatei verwenden.
- Erstelle eine Datei <filename>application/configs/application.ini</filename>, und füge
- den folgenden Inhalt ein:
- </para>
- <programlisting language="dosini"><![CDATA[
- [production]
- phpSettings.display_startup_errors = 0
- phpSettings.display_errors = 0
- includePaths.library = APPLICATION_PATH "/../library"
- bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
- bootstrap.class = "Bootstrap"
- resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
- [staging : production]
- [testing : production]
- phpSettings.display_startup_errors = 1
- phpSettings.display_errors = 1
- [development : production]
- phpSettings.display_startup_errors = 1
- phpSettings.display_errors = 1
- ]]></programlisting>
- <para>
- Jetz verändern wir das Gateway Skript <filename>public/index.php</filename>. Wenn die
- Datei nicht existiert, erstellen wir sie; andernfalls ersetzen wir sie mit dem folgenden
- Inhalt:
- </para>
- <programlisting language="php"><![CDATA[
- // Define path to application directory
- defined('APPLICATION_PATH')
- || define('APPLICATION_PATH',
- realpath(dirname(__FILE__) . '/../application'));
- // Define application environment
- defined('APPLICATION_ENV')
- || define('APPLICATION_ENV',
- (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
- : 'production'));
- // Typischerweise wird man das eigene library/ Verzeichnis zum include_path
- // hinzufügen wollen, speziell wenn es die ZF Installation enthält
- set_include_path(implode(PATH_SEPARATOR, array(
- dirname(dirname(__FILE__)) . '/library',
- get_include_path(),
- )));
- /** Zend_Application */
- require_once 'Zend/Application.php';
- // Create application, bootstrap, and run
- $application = new Zend_Application(
- APPLICATION_ENV,
- APPLICATION_PATH . '/configs/application.ini'
- );
- $application->bootstrap()
- ->run();
- ]]></programlisting>
- <para>
- Es ist zu beachten, dass die Umgebungskonstante der Anwendung nach einer
- Umgebungsvariablen "APPLICATION_ENV" sucht. Wir empfehlen diese im Web Server Environment
- zu setzen. In Apache kann man diese entweder in der vhost Definition setzen, oder in der
- Datei <filename>.htaccess</filename>. Wir empfehlen den folgenden Inhalt in der Datei
- <filename>public/.htaccess</filename>:
- </para>
- <programlisting language="conf"><![CDATA[
- SetEnv APPLICATION_ENV development
- RewriteEngine On
- RewriteCond %{REQUEST_FILENAME} -s [OR]
- RewriteCond %{REQUEST_FILENAME} -l [OR]
- RewriteCond %{REQUEST_FILENAME} -d
- RewriteRule ^.*$ - [NC,L]
- RewriteRule ^.*$ index.php [NC,L]
- ]]></programlisting>
- <note>
- <title>Mehr über mod_rewrite lernen</title>
- <para>
- Die obigen Rewrite-Regeln erlauben es, auf jede Datei im DocumentRoot des eigenen
- virtuellen Hosts zuzugreifen. Wenn es Dateien gibt, die man auf diesem Weg nicht
- bereitstellen will, muss man in seinen Regeln restriktiver sein. Gehe zur
- Apache WebSite und <ulink
- url="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html">lerne mehr über
- mod_rewrite</ulink>.
- </para>
- </note>
- <para>
- An diesem Punkt haben wir es geschafft und können damit beginnen, die Vorteile von
- <classname>Zend_Application</classname> zu nutzen.
- </para>
- </sect2>
- <sect2 id="zend.application.quick-start.resources">
- <title>Hinzufügen und Erstellen von Ressourcen</title>
- <para>
- Wenn man den Anleitungen von oben gefolgt ist, dann verwendet die Bootstrap-Klasse
- einen FrontController, und wenn sie gestartet wird, wird sie den FrontController
- ausführen. Trotzdem wird man mit großer Wahrscheinlichkeit etwas mehr Konfiguration
- als das benötigen.
- </para>
- <para>
- In diesem Kapitel werden wir zwei Ressourcen zur Anwendung hinzufügen. Zuerst werden
- wir Layouts erstellen, und dann werden wir das View Objekt anpassen.
- </para>
- <para>
- Eine der von <classname>Zend_Application</classname> angebotenen Standardressourcen ist
- die "layout" Ressource. Diese Ressource erwartet, dass man Konfigurationswerte definiert,
- welche dann verwendet werden, um unsere Instanz von <classname>Zend_Layout</classname>
- zu konfigurieren.
- </para>
- <para>
- Um sie zu verwenden, müssen wir die Konfigurationsdatei aktualisieren.
- </para>
- <programlisting language="dosini"><![CDATA[
- [production]
- phpSettings.display_startup_errors = 0
- phpSettings.display_errors = 0
- bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
- bootstrap.class = "Bootstrap"
- resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
- ; ADD THE FOLLOWING LINES
- resources.layout.layout = "layout"
- resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
- [staging : production]
- [testing : production]
- phpSettings.display_startup_errors = 1
- phpSettings.display_errors = 1
- [development : production]
- phpSettings.display_startup_errors = 1
- phpSettings.display_errors = 1
- ]]></programlisting>
- <para>
- Wenn man es nicht bereits getan hat, muß man das Verzeichnis
- <filename>application/layouts/scripts/</filename> und die Datei
- <filename>layout.phtml</filename> in diesem Verzeichnis erstellen. Ein gutes Layout zum
- Starten ist das folgende (und ist mit der View Ressource verbunden, die als nächstes
- besprochen wird):
- </para>
- <programlisting language="php"><![CDATA[
- <?php echo $this->doctype() ?>
- <html>
- <head>
- <?php echo $this->headTitle() ?>
- <?php echo $this->headLink() ?>
- <?php echo $this->headStyle() ?>
- <?php echo $this->headScript() ?>
- </head>
- <body>
- <?php echo $this->layout()->content ?>
- </body>
- </html>
- ]]></programlisting>
- <para>
- An diesem Punkt haben wir ein funktionierendes Layout.
- </para>
- <para>
- Jetzt fügen wir eine eigene View Ressource hinzu. Wenn die View initialisiert wird,
- will man den <acronym>HTML</acronym> DocType und einen Standardwert für den im
- <acronym>HTML</acronym>-Kopf zu verwendenden Titel setzen. Das kann durch die Änderung
- der <classname>Bootstrap</classname>-Klasse und dem Hinzufügen einer Methode gemacht
- werden:
- </para>
- <programlisting language="php"><![CDATA[
- class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
- {
- protected function _initView()
- {
- // View initialisieren
- $view = new Zend_View();
- $view->doctype('XHTML1_STRICT');
- $view->headTitle('My First Zend Framework Application');
- // View zum ViewRenderer hinzufügen
- $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
- 'ViewRenderer'
- );
- $viewRenderer->setView($view);
- // Zurückgeben, damit sie von Bootstrap gespeichert werden kann
- return $view;
- }
- }
- ]]></programlisting>
- <para>
- Diese Methode wird automatisch ausgeführt, wenn das Bootstrap der Anwendung ausgeführt
- wird und stellt sicher, dass die View so initialisiert wird, wie die Anwendung das
- benötigt.
- </para>
- </sect2>
- <sect2 id="zend.application.quick-start.next-steps">
- <title>Nächste Schritte mit Zend_Application</title>
- <para>
- Das oben erwähnte reicht, um mit <classname>Zend_Application</classname> zu beginnen und
- das Bootstrap der eigenen Anwendung zu erstellen. Von hier an sollte man beginnen
- Ressource-Methoden zu erstellen, oder für maximale Wiederverwendbarkeit,
- Ressource-Plugin Klassen. Lesen Sie weiter, um mehr zu lernen!
- </para>
- </sect2>
- </sect1>
|