Zend Framework教程之Application和Bootstrap用法详解(2)

较新版本的zend framework引入了Zend_Application和Bootstrap。Zend_Application 提供了一个可重用资源的引导,通用和模块化的引导类和依赖检查。 同时默认负责设置 PHP 环境变量和自动加载功能。

默认新建项目后,会给出如下的几个文件:

1.项目的Bootstrap

first_web/
├── application
│   ├── Bootstrap.php

具体代码如下:

<?php class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { }

2.配置文件

│   ├── configs
│   │   └── application.ini

具体代码如下:

[production] phpSettings.display_startup_errors = 0 phpSettings.display_errors = 0 includePaths.library = APPLICATION_PATH "/../library" bootstrap.path = APPLICATION_PATH "/Bootstrap.php" bootstrap.class = "Bootstrap" appnamespace = "Application" resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" resources.frontController.params.displayExceptions = 0 [staging : production] [testing : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 [development : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 resources.frontController.params.displayExceptions = 1

3.项目入口文件

├── public
│   └── index.php

<?php // 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')); // Ensure library/ is on include_path set_include_path(implode(PATH_SEPARATOR, array( realpath(APPLICATION_PATH . '/../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();

以上代码就是基本的使用Zend_Application方式,完成了环境变量的初始化,加载配置文件,初始化环境,加载模块,完成web应用程序的引导功能。

资源插件

资源插件只需要实现Zend_Application_Resource_Resource,或者,更简单的是,继承Zend_Application_Resource_ResourceAbstract。接口如下:

interface Zend_Application_Resource_Resource { public function __construct($options = null); public function setBootstrap( Zend_Application_Bootstrap_Bootstrapper $bootstrap ); public function getBootstrap(); public function setOptions(array $options); public function getOptions(); public function init(); }

例如

class My_Resource_View extends Zend_Application_Resource_ResourceAbstract { protected $_view; public function init() { // Return view so bootstrap will store it in the registry return $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; } }

加载使用资源插件

我了提供资源的重用性,可以将资源方法定义为资源插件。。

为了让bootstrap能够识别资源插件,定义资源插件时,需要实现Zend_Application_Bootstrap_ResourceBootstrapper. 接口定义了查找插件,注册插件和加载插件的api:

interface Zend_Application_Bootstrap_ResourceBootstrapper { public function registerPluginResource($resource, $options = null); public function unregisterPluginResource($resource); public function hasPluginResource($resource); public function getPluginResource($resource); public function getPluginResources(); public function getPluginResourceNames(); public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader); public function getPluginLoader(); }

采用资源插件,不仅可以让资源可以重复利用,同时让bootstrap更简洁,如果要修改,新增资源也无需修改你的bootstrap。

通过实现Zend_Application_Bootstrap_BootstrapAbstract (被 Zend_Application_Bootstrap_Bootstrap 继承) ,才可以使用定义的资源插件

通过将指定的选项传递到application object and/or bootstrap,来注册使用资源插件。这些选项可能会从一个配置文件,或通过手动指定。规则是选项​​必须是键值对,键代表资源名称。资源名称,是资源插件类的类前缀。例如,Zend框架自带的资源类前缀“Zend_Application_Resource_”;任何以下,这都是资源的名称。例如,

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/bc570aad8b9028bbbf2dc6f5d7a5450b.html