Einführung

Zend_Application Quick Start

Es gibt zwei Wege um mit Zend_Application anzufangen, und diese hängen davon ab wie man das Projekt beginnt. In jedem Fall beginnt man immer mit der Erstellung einer Bootstrap Klasse und einer diesbezüglichen Konfigurationsdatei.

Wenn man plant Zend_Tool zu verwenden um das eigene Projekt zu erstellen sollte man anbei weiterlesen. Wenn man Zend_Application zu einem existierenden Projekt hinzufügen will, sollte man hier weiterlesen.

Verwenden von Zend_Tool

Der schnellste Weg um mit Zend_Application zu beginnen ist die Verwendung von Zend_Tool um das Projekt zu erstellen. Das erstellt auch die Bootstrap Klasse und die Datei.

Um ein Projekt zu erstellen muß einfach das zf Kommando (auf *nix Systemen) ausgeführt werden:

  1. % zf create project newproject

Oder unter Windows das zf.bat Kommando:

  1. C:> zf.bat create project newproject

Beides erstellt eine Projektstruktur die wie folgt aussieht:

  1. newproject
  2. |-- application
  3. |   |-- Bootstrap.php
  4. |   |-- configs
  5. |   |   `-- application.ini
  6. |   |-- controllers
  7. |   |   |-- ErrorController.php
  8. |   |   `-- IndexController.php
  9. |   |-- models
  10. |   `-- views
  11. |       |-- helpers
  12. |       `-- scripts
  13. |           |-- error
  14. |           |   `-- error.phtml
  15. |           `-- index
  16. |               `-- index.phtml
  17. |-- library
  18. |-- public
  19. |   `-- index.php
  20. `-- tests
  21.     |-- application
  22.     |   `-- bootstrap.php
  23.     |-- library
  24.     |   `-- bootstrap.php
  25.     `-- phpunit.xml

Im obigen Diagramm ist die Bootstrap unter newproject/application/Bootstrap.php und sieht zuerst wie folgt aus:

  1. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  2. {
  3. }

Es ist auch zu bemerken das eine Konfigurationdatei unter newproject/application/configs/application.ini erstellt wurde. Diese hat den folgenden Inhalt:

  1. [production]
  2. phpSettings.display_startup_errors = 0
  3. phpSettings.display_errors = 0
  4. includePaths.library = APPLICATION_PATH "/../library"
  5. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  6. bootstrap.class = "Bootstrap"
  7. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  8.  
  9. [staging : production]
  10.  
  11. [testing : production]
  12. phpSettings.display_startup_errors = 1
  13. phpSettings.display_errors = 1
  14.  
  15. [development : production]
  16. phpSettings.display_startup_errors = 1
  17. phpSettings.display_errors = 1

Alle Einstellungen in dieser Konfigurationsdatei sind für die Verwendung mit Zend_Application und der Bootstrap.

Eine andere Datei von Interesse ist die newproject/public/index.php Datei, welche Zend_Application einfügt und diese ausführt.

  1. // Define path to application directory
  2. defined('APPLICATION_PATH')
  3.     || define('APPLICATION_PATH',
  4.               realpath(dirname(__FILE__) . '/../application'));
  5.  
  6. // Define application environment
  7. defined('APPLICATION_ENV')
  8.     || define('APPLICATION_ENV',
  9.               (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
  10.                                          : 'production'));
  11.  
  12. /** Zend_Application */
  13. require_once 'Zend/Application.php';
  14.  
  15. // Create application, bootstrap, and run
  16. $application = new Zend_Application(
  17.     APPLICATION_ENV,
  18.     APPLICATION_PATH . '/configs/application.ini'
  19. );
  20. $application->bootstrap()
  21.             ->run();

Um mit dem Quick Start weiterzumachen, springen Sie bitte auf das Ressource Kapitel.

Zend_Application in der eigenen Anwendung hinzufügen

Die Basis von Zend_Application ist wirklich einfach:

  • Eine application/Bootstrap.php Datei mit der Klasse Bootstrap erstellen.

  • Eine Konfigurationsdatei application/configs/application.ini mit der Basiskonfiguration für Zend_Application erstellen.

  • Ändern von public/index.php um Zend_Application anzupassen.

Zuerst die eigene Bootstrap Klasse erstellen. Erzeuge eine Datei application/Bootstrap.php mit dem folgenden Inhalt:

  1. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  2. {
  3. }

Jetzt die Konfiguration erstellen. Für dieses Tutorial, verwenden wir eine Konfiguration im INI Stil; man kann natürlich genauso eine XML oder PHP Konfigurationsdatei verwenden. Erstelle eine Datei application/configs/application.ini, und füge den folgenden Inhalt ein:

  1. [production]
  2. phpSettings.display_startup_errors = 0
  3. phpSettings.display_errors = 0
  4. includePaths.library = APPLICATION_PATH "/../library"
  5. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  6. bootstrap.class = "Bootstrap"
  7. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  8.  
  9. [staging : production]
  10.  
  11. [testing : production]
  12. phpSettings.display_startup_errors = 1
  13. phpSettings.display_errors = 1
  14.  
  15. [development : production]
  16. phpSettings.display_startup_errors = 1
  17. phpSettings.display_errors = 1

Jetz verändern wir das Gateway Skript public/index.php. Wenn die Datei nicht existiert erstellen wir Sie; andernfalls ersetzen wir Sie mit dem folgenden Inhalt:

  1. // Define path to application directory
  2. defined('APPLICATION_PATH')
  3.     || define('APPLICATION_PATH',
  4.               realpath(dirname(__FILE__) . '/../application'));
  5.  
  6. // Define application environment
  7. defined('APPLICATION_ENV')
  8.     || define('APPLICATION_ENV',
  9.               (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
  10.                                          : 'production'));
  11.  
  12. // Typischerweise wird man das eigene library/ Verzeichnis zum include_path
  13. // hinzufügen wollen, speziell wenn es die ZF Installation enthält
  14. set_include_path(implode(PATH_SEPARATOR, array(
  15.     dirname(dirname(__FILE__)) . '/library',
  16. )));
  17.  
  18. /** Zend_Application */
  19. require_once 'Zend/Application.php';
  20.  
  21. // Create application, bootstrap, and run
  22. $application = new Zend_Application(
  23.     APPLICATION_ENV,
  24.     APPLICATION_PATH . '/configs/application.ini'
  25. );
  26. $application->bootstrap()
  27.             ->run();

Es ist zu beachten das die Environment Konstante der Anwendung nach einer Environment Variable "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 .htaccess Datei. Wir empfehlen den folgenden Inhalt in der Datei public/.htaccess:

  1. SetEnv APPLICATION_ENV development
  2.  
  3. RewriteEngine On
  4. RewriteCond %{REQUEST_FILENAME} -s [OR]
  5. RewriteCond %{REQUEST_FILENAME} -l [OR]
  6. RewriteCond %{REQUEST_FILENAME} -d
  7. RewriteRule ^.*$ - [NC,L]
  8. RewriteRule ^.*$ index.php [NC,L]

Note: Mehr über mod_rewrite lernen
Die obigen Rewrite Regeln erlauben es auf jede Datei im Document Root des eigenen virtuellen Host's 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 » lerne mehr über mod_rewrite.

An diesem Punkt, wurde alles gesetzt um die Vorteile von Zend_Application zu verwenden.

Hinzufügen und Erstellen von Ressourcen

Wenn man den Anleitungen von oben gefolgt ist, dann verwendet die Bootstrap Klasse einen Front Controller, und wenn Sie gestartet wird, wird Sie den Front Controller ausführen. Trotzdem wird man, in allen Fällen, etwas mehr Konfiguration als das benötigen.

In diesem Kapitel werden wir zwei Ressourcen zur anwendung hinzufügen. Zuerst werden wir Layouts erstellen, und dann werden wir ein View Objekt anpassen.

Eine der von Zend_Application angebotenen Standardressourcen ist die "layout" Ressource. Diese Ressource erwartet, das man Konfgurationswerte definiert, welche dann verwendet werden um die Zend_Layout Instanz zu konfigurieren.

Um Sie zu verwenden müssen wir die Konfigurationsdatei aktualisieren.

  1. [production]
  2. phpSettings.display_startup_errors = 0
  3. phpSettings.display_errors = 0
  4. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  5. bootstrap.class = "Bootstrap"
  6. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  7. ; ADD THE FOLLOWING LINES
  8. resources.layout.layout = "layout"
  9. resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
  10.  
  11. [staging : production]
  12.  
  13. [testing : production]
  14. phpSettings.display_startup_errors = 1
  15. phpSettings.display_errors = 1
  16.  
  17. [development : production]
  18. phpSettings.display_startup_errors = 1
  19. phpSettings.display_errors = 1

Wenn man es nicht bereits getan hat, muß man das Verzeichnis application/layouts/scripts/ und die Datei layout.phtml in diesem Verzeichnis erstellen. Ein gutes Layout zum Starten ist das folgende (und ist mit der View Ressource die als nächstes besprochen wird verbunden):

  1. <?php echo $this->doctype() ?>
  2. <html>
  3. <head>
  4.     <?php echo $this->headTitle() ?>
  5.     <?php echo $this->headLink() ?>
  6.     <?php echo $this->headStyle() ?>
  7.     <?php echo $this->headScript() ?>
  8. </head>
  9. <body>
  10.     <?php echo $this->layout()->content ?>
  11. </body>
  12. </html>

An diesem Punkt, hat man ein funktionierendes Layout.

Jetzt fügen wir eine eigene View Ressource hinzu. Wenn die View initialisiert wird, will man den HTML DocType und einen Standardwert für den im HTML Kopf zu verwendenden Titel setzen. Das kann durch die Änderung der Bootstrap Klasse und dem Hinzufügen einer Methode gemacht werden:

  1. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  2. {
  3.     protected function _initView()
  4.     {
  5.         // Initialize view
  6.         $view = new Zend_View();
  7.         $view->doctype('XHTML1_STRICT');
  8.         $view->headTitle('My First Zend Framework Application');
  9.  
  10.         // Add it to the ViewRenderer
  11.         $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
  12.             'ViewRenderer'
  13.         );
  14.         $viewRenderer->setView($view);
  15.  
  16.         // Return it, so that it can be stored by the bootstrap
  17.         return $view;
  18.     }
  19. }

Diese Methode wird automatisch ausgeführt wenn das Bootstrap der Anwendung ausgeführt wird und stellt sicher das die View so initialisiert wird wie die Anwendung das benötigt.

Nächste Schritte mit Zend_Application

Das oben erwähnte reicht um mit Zend_Application 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!


Einführung