Theorie der Funktionsweise Um eine MVC Anwendung zu konfigurieren und bereit zur Auslieferung zu bekommen wird ein immer größerer Anteil an Code benötigt sobald mehr Features verfügbar sind: Setzen der Datenbank, Konfiguration der View und View Helfer, Konfiguration der Layouts, Registrierung der Plugins, Registrierung der Aktion Helfer, und andere. Zusätzlich will man oft den gleichen Code wiederverwenden um Tests zu bootstrappen, einen Cronjob, oder ein Service Skript zu haben. Wärend es sehr einfach ist sein Bootstrap Skript einzubauen, gibt es oft Initialisierungen die abhängig sind von der Umgebung - man könnte kein MVC für einen Cronjob benötigen, oder nur den DB Layer für ein Service Skript. Zend_Application zielt darauf ab das alles einfacher zu machen und bietet Wiederwervendbarkeit durch die Kapselung vom Bootstrapping in OOP Paradigmen. Zend_Application ist in drei Bereiche geteilt: Zend_Application: Lädt die PHP Umgebung, inklusive Inklude-Pfade und Autoloading, und instanziiert die benötigte Bootstrap Klasse. Zend_Application_Bootstrap: Bietet Interfaces für Bootstrap Klassen. Zend_Application_Bootstrap_Bootstrap bietet übliche Funktionalitäten für die meisten Notwendigkeiten des Bootstrappings, inklusive Angorithmen um Abhängigkeiten zu prüfen und die Möglichkeit Bootstrap Ressourcen bei Bedarf zu laden. Zend_Application_Resource bietet ein Interface für Standard Bootstrap Ressourcen die bei Bedarf von einer Bootstrap Instanz geladen werden können, sowie verschiedene standardmäßige Ressource Implementationen. Entwickler erstellen eine Bootstrap Klasse für Ihre Anwendung und erweitern Zend_Application_Bootstrap_Bootstrap oder implementieren (mindestens) Zend_Application_Bootstrap_BootstrapAbstract. Der Einstiegspunkt (z.B. public/index.php) lädt Zend_Application und initialisiert Sie, indem folgendes übergeben wird: Die aktuelle Umgebung Optionen für das Bootstrapping Die Bootstrap Optionen enthalten den Pfad zur Datei die die Bootstrap Klasse enthält und optional: Jeden include_path der extra zu setzen ist Jeden Autoloader Namespace der zusätzlich zu registrieren ist Jede php.ini Einstellung die zu initialisieren ist Den Klassennamen für die Bootstrap Klasse (wenn diese nicht "Bootstrap" ist) Ressourcen Präfix/Pfad Paare die zu verwenden sind Jede Ressource die zu verwenden ist (durch Name der Klasse oder Kurzname) Zusätzliche Pfade zu einer Konfigurationsdatei die zu laden ist Zusätzliche Konfigurations Optionen Optionen können, ein Array, ein Zend_Config Objekt, oder der Pfad zu einer Konfigurationsdatei sein. Bootstrapping Zend_Application's zweites Feld der Arbeit ist die Ausführung des Bootstraps der Anwendung. Bootstraps müssen mindestens Zend_Application_Bootstrap_Bootstrapper implementieren, welches die folgende API definiert: Diese API erlaubt es dem Bootstrap die Umgebung und die Konfiguration vom Objekt der Anwendung zu akzeptieren, die Ressourcen die für das Bootstrapping verantwortlich sind zu melden, und dann das Bootstrappen ausführen und die Anwendung zu starten. Man kann dieses Interface selbst implementieren, Zend_Application_Bootstrap_BootstrapAbstract erweitern, oder Zend_Application_Bootstrap_Bootstrap verwenden. Neben dieser Funktionalität gibt es eine Anzahl anderer Gebiete von Interesse mit denen man vertraut sein sollte. Methoden für Ressourcen Die Implementation von Zend_Application_Bootstrap_BootstrapAbstract bietet eine einfache Konvention für die Definition von Methoden für Ressourcen Klassen. Jede geschützte Methode (protected), deren Name mit _init beginnt wird als Ressourcemethode angenommen. Um eine einzelne Ressource Methode zu bootstrappen, muß die bootstrap() Methode verwendet, und der Name der Ressource übergeben werden. Der Name ist der Name der Methode ohne den _init Präfix. Um mehrere Ressourcen Methoden zu bootstrappen, muß ein Array von Namen übergeben werden. Um alle Ressourcen Methoden zu bootstrappen, darf nichts übergeben werden. Nehmen wir die folgende Bootstrap Klasse an: Um nur die _initFoo() Methode zu bootstrappen, machen wir das folgende: bootstrap('foo'); ]]> Um die Methoden _initFoo() und _initBar() zu bootstrappen, machen wir das folgende: bootstrap(array('foo', 'bar)); ]]> Um alle Ressourcen Methoden zu bootstrappen, rufen wir bootstrap() ohne Argumente auf: bootstrap(); ]]> Bootstraps die Ressourcen Plugins benutzen To make your bootstraps more re-usable, we have provided the ability to push your resources into resource plugin classes. This allows you to mix and match resources simply via configuration. We will cover how to create resources later; in this section we will show you how to utilize them only. If your bootstrap should be capable of using resource plugins, you will need to implement an additional interface, Zend_Application_Bootstrap_ResourceBootstrapper. This interface defines an API for locating, registering, and loading resource plugins: Resource plugins basically provide the ability to create resource intializers that can be re-used between applications. This allows you to keep your actual bootstrap relatively clean, and to introduce new resources without needing to touch your bootstrap itself. Zend_Application_Bootstrap_BootstrapAbstract (and Zend_Application_Bootstrap_Bootstrap by extension) implement this interface as well, allowing you to utilize resource plugins. To utilize resource plugins, you must specify them in the options passed to the application object and/or bootstrap. These options may come from a configuration file, or be passed in manually. Options will be of key/options pairs, with the key representing the resource name. The resource name will be the segment following the class prefix. For example, the resources shipped with Zend Framework have the class prefix "Zend_Application_Resource_"; anything following this would be the name of the resource. As an example, array( 'FrontController' => array( 'controllerDirectory' => APPLICATION_PATH . '/controllers', ), ), )); ]]> This indicates that the "FrontController" resource should be used, with the options specified. If you begin writing your own resource plugins, or utilize third-party resource plugins, you will need to tell your bootstrap where to look for them. Internally, the bootstrap utilizes Zend_Loader_PluginLoader, so you will only need to indicate the common class prefix an path pairs. As an example, let's assume you have custom resource plugins in APPLICATION_PATH/resources/ and that they share the common class prefix of My_Resource. You would then pass that information to the application object as follows: array( 'My_Resource' => APPLICATION_PATH . '/resources/', ), 'resources' => array( 'FrontController' => array( 'controllerDirectory' => APPLICATION_PATH . '/controllers', ), ), )); ]]> You would now be able to use resources from that directory. Just like resource methods, you use the bootstrap() method to execute resource plugins. Just like with resource methods, you can specify either a single resource plugin, multiple plugins (via an array), or all plugins. Additionally, you can mix and match to execute resource methods as well. bootstrap('FrontController'); // Execute several: $bootstrap->bootstrap(array('FrontController', 'Foo')); // Execute all resource methods and plugins: $bootstrap->bootstrap(); ]]> Resource Registry Many, if not all, of your resource methods or plugins will initialize objects, and in many cases, these objects will be needed elsewhere in your application. How can you access them? Zend_Application_Bootstrap_BootstrapAbstract provides a local registry for these objects. To store your objects in them, you simply return them from your resources. For maximum flexibility, this registry is referred to as a "container" internally; its only requirements are that it is an object. Resources are then registered as properties named after the resource name. By default, an instance of Zend_Registry is used, but you may also specify any other object you wish. The methods setContainer() and getContainer() may be used to manipulate the container itself. getResource($resource) can be used to fetch a given resource from the container, and hasResource($resource) to check if the resource has actually been registered. As an example, consider a basic view resource: You can then check for it and/or fetch it as follows: hasResource('view')) { $view = $bootstrap->getResource('view'); } // Via the container: $container = $bootstrap->getContainer(); if (isset($container->view)) { $view = $container->view; } ]]> Please note that the registry/container is not global. This means that you need access to the bootstrap in order to fetch resources. Zend_Application_Bootstrap_Bootstrap provides some convenience for this: during its run() execution, it registers itself as the front controller parameter "bootstrap", which allows you to fetch it from the router, dispatcher, plugins, and action controllers. As an example, if you wanted access to the view resource from above within your action controller, you could do the following: getInvokeArg('bootstrap'); $view = $bootstrap->getResource('view'); // ... } } ]]> Dependency Tracking In addition to executing resource methods and plugins, it's necessary to ensure that these are executed once and once only; these are meant to bootstrap an application, and executing multiple times can lead to resource overhead. At the same time, some resources may depend on other resources being executed. To solve these two issues, Zend_Application_Bootstrap_BootstrapAbstract provides a simple, effective mechanism for dependency tracking. As noted previously, all resources -- whether methods or plugins -- are bootstrapped by calling bootstrap($resource), where $resource is the name of a resource, an array of resources, or, left empty, indicates all resources should be run. If a resource depends on another resource, it should call bootstrap() within its code to ensure that resource has been executed. Subsequent calls to it will then be ignored. In a resource method, such a call would look like this: bootstrap('FrontController'); // Retrieve the front controller from the bootstrap registry $front = $this->getResource('FrontController'); $request = new Zend_Controller_Request_Http(); $request->setBaseUrl('/foo'); $front->setRequest($request); // Ensure the request is stored in the bootstrap registry return $request; } } ]]> Resource Plugins As noted previously, a good way to create re-usable bootstrap resources and to offload much of your coding to discrete classes is to utilize resource plugins. While Zend Framework ships with a number of standard resource plugins, the intention is that developers should write their own to encapsulate their own intialization needs. Resources need only implement Zend_Application_Resource_Resource, or, more simply still, extend Zend_Application_Resource_ResourceAbstract. The basic interface is simply this: The interface defines simply that a resource should accept options to the constructor, have mechanisms for setting and retrieving options, have mechanisms for setting and retrieving the bootstrap object, and an initialization method. As an example, let's assume you have a common view intialization you use in your applications. You have a common doctype, CSS and JavaScript, and you want to be able to pass in a base document title via configuration. Such a resource might look like this: getView(); } public function getView() { if (null === $this->_view) { $options = $this->getOptions(); $title = ''; if (array_key_exists('title', $options)) { $title = $options['title']; unset($options['title']); } $view = new Zend_View($options); $view->doctype('XHTML1_STRICT'); $view->headTitle($title); $view->headLink()->appendStylesheet('/css/site.css'); $view->headScript()->appendfile('/js/analytics.js'); $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper( 'ViewRenderer', ); $viewRenderer->setView($view); $this->_view = $view; } return $this->_view; } } ]]> As long as you register the prefix path for this resource plugin, you can then use it in your application. Even better, because it uses the plugin loader, you are effectively overriding the shipped "View" resource plugin, ensuring that your own is used instead.