


Detailed explanation of Application and Bootstrap usage in Zend Framework tutorial
Dec 27, 2016 pm 02:36 PMThe examples in this article describe the usage of Application and Bootstrap in the Zend Framework tutorial. Share it with everyone for your reference, as follows:
In an MVC application, we need to initialize and establish a database link, configure views and view assistants, configure layout, register related plug-ins, register action assistants, etc., these We need to complete the configuration and preparation work one by one. Sometimes some initialization operations may be required, but in some cases these initialization may not be needed. Zend_Application can not only complete these operations, but also make these configuration and initialization tasks more unified and orderly, with higher reusability.
Zend_Application usage can be subdivided into three types:
Zend_Application: loads the PHP environment, including include_paths and automatic loading, and instantiates the boot class.
Zend_Application_Bootstrap: Provides an interface for boot classes.
Zend_Application_Bootstrap_Bootstrap completes most common functions that boot needs to provide, including dependency checking and loading boot resources on demand.
Zend_Application_Resource provides on-demand resource loading function
Developers can inherit Zend_Application_Bootstrap_Bootstrap or implement the Zend_Application_Bootstrap_Bootstrapper interface as needed. Zend_Application is loaded in the entry file (for example, public/index.php) and instantiated according to the boot options and current environment configuration.
Boot options include the specified boot class file and boot class path. The options are as follows:
Required include_paths
The automatic loading function additionally loads the registered namespace
php.ini initialization settings
Specify the bootstrap class name, if it is not "Bootstrap"
The resource prefix key-value pair key represents the resource prefix name
The resource Class name or alias
Additional loaded configuration file path
Additional configuration options
Options can be an array, or a Zend_Config object, or a configuration file at a specified location
Bootstrapper
The second function of Zend_Application is to guide the application. Bootstraps must implement the Zend_Application_Bootstrap_Bootstrapper interface. The specific interface API is as follows:
interface Zend_Application_Bootstrap_Bootstrapper { public function __construct($application); public function setOptions(array $options); public function getApplication(); public function getEnvironment(); public function getClassResources(); public function getClassResourceNames(); public function bootstrap($resource = null); public function run(); }
api mainly provides environment configuration and obtains guidance. Loaded resources, and bootstrap
You can implement the interface or inherit Zend_Application_Bootstrap_BootstrapAbstract, or Zend_Application_Bootstrap_Bootstrap.
Resource method
The resource method that implements the Zend_Application_Bootstrap_BootstrapAbstract interface must follow the following rules. Method The type is protected, and the prefix of the method must start with _init will.
If you want to load and use a resource method, just add the resource name in bootstrap(). The resource name is the resource method without the _init prefix.
If you want to load multiple resource methods, you can specify them through an array.
For example, bootstrap class:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { protected function _initFoo() { // ... } protected function _initBar() { // ... } protected function _initBaz() { // ... } }
Only load using _initFoo():
$bootstrap->bootstrap('foo');
Load using _initFoo() and _initBar():
$bootstrap->bootstrap(array('foo', 'bar'));
Load and use all resource methods, use bootstrap() without parameters:
$bootstrap->bootstrap();
New first_web project
root@coder-671T-M:/mydev_src/zend_framework_learn/www# tree first_web/
first_web/
├── application
│ ├── Bootstrap.php
│ ├── configs
│ │ └── application.ini
│ ├── controllers
| scripts
│?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? error #│ └ ── README.txt
├── library
├── public
│ └── index.php
└── tests
├── application
│ └── controllers
│ └── IndexControllerTest.php
├── bootstrap.php
├── library
└── phpunit.xml
16 directories, 11 files
Newer versions of zend framework introduced Zend_Application and Bootstrap. Zend_Application provides a bootstrap of reusable resources, generic and modular bootstrap classes and dependency checks. It is also responsible for setting PHP environment variables and automatic loading functions by default.
By default, after creating a new project, the following files will be given:
1. Bootstrap of the project
first_web/
├── application
│ ├── Bootstrap.php
The specific code is as follows:
<?php class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { }
2. Configuration file
│ ├── 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方式,完成了環(huán)境變量的初始化,加載配置文件,初始化環(huán)境,加載模塊,完成web應(yīng)用程序的引導(dǎo)功能。
資源插件
資源插件只需要實現(xiàn)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能夠識別資源插件,定義資源插件時,需要實現(xiàn)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(); }
采用資源插件,不僅可以讓資源可以重復(fù)利用,同時讓bootstrap更簡潔,如果要修改,新增資源也無需修改你的bootstrap。
通過實現(xiàn)Zend_Application_Bootstrap_BootstrapAbstract (被 Zend_Application_Bootstrap_Bootstrap 繼承) ,才可以使用定義的資源插件
通過將指定的選項傳遞到application object and/or bootstrap,來注冊使用資源插件。這些選項可能會從一個配置文件,或通過手動指定。規(guī)則是選項必須是鍵值對,鍵代表資源名稱。資源名稱,是資源插件類的類前綴。例如,Zend框架自帶的資源類前綴“Zend_Application_Resource_”;任何以下,這都是資源的名稱。例如,
$application = new Zend_Application(APPLICATION_ENV, array( 'resources' => array( 'FrontController' => array( 'controllerDirectory' => APPLICATION_PATH . '/controllers', ), ), ));
"FrontController"資源是個特例。他的選項比較特殊。
無論是使用自己的寫的資源插件還是使用第三方的資源插件。你必須保證bootstrap能找到他們,bootstrap 內(nèi)部通過 Zend_Loader_PluginLoader可以讓我們只需要指定資源插件的類前綴,值為資源類的類路徑便可以輕松注冊資源插件。
例如,如果編寫的資源插件存放在APPLICATION_PATH/resources/ 下,類前綴為My_Resource. 可以使用如下方法注冊加載:
$application = new Zend_Application(APPLICATION_ENV, array( 'pluginPaths' => array( 'My_Resource' => APPLICATION_PATH . '/resources/', ), 'resources' => array( 'FrontController' => array( 'controllerDirectory' => APPLICATION_PATH . '/controllers', ), ), ));
在應(yīng)用程序中比可以使用指定目錄下的資源。
和資源方法類似,通過使用 the bootstrap() 方法可以加載資源插件。加載單一,多個,全部的資源插件的配置方式也類似.
例如:
// Execute one: $bootstrap->bootstrap('FrontController'); // Execute several: $bootstrap->bootstrap(array('FrontController', 'Foo')); // Execute all resource methods and plugins: $bootstrap->bootstrap();
資源注冊表
為了避免資源的重復(fù)注冊,導(dǎo)致不必要的浪費Zend_Application_Bootstrap_BootstrapAbstract 提供了一個本地注冊表對象存儲這些資源對象.當(dāng)你想要存放一個資源的時候,只需要在方法中返回這個資源即可。
為了靈活性,注冊表是作為一個內(nèi)部“容器”存在的。只要是對象都可以存入到容器中。資源名稱對應(yīng)為容器的屬性。默認情況下,可以通過Zend_Registry獲取實例使用,也可以自定義其他對象。 setContainer() and getContainer() 方法可用于操縱容器本身。getResource($resource) 可用于獲取一個指定資源。hasResource($resource) 可以檢查資源是否已經(jīng)注冊存在
例如,注冊一個view資源:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { protected function _initView() { $view = new Zend_View(); // more initialization... return $view; } }
資源的相關(guān)操作:
// Using the has/getResource() pair: if ($bootstrap->hasResource('view')) { $view = $bootstrap->getResource('view'); } // Via the container: $container = $bootstrap->getContainer(); if (isset($container->view)) { $view = $container->view; }
請注意:注冊表容器是不是全局的。這意味著你需要通過訪問的bootstrap來獲取資源。 Zend_Application_Bootstrap_Bootstrap提供了 run() , 執(zhí)行了 run() 之后,它會注冊為前端控制器參數(shù)的“bootstrap”,通過他可以獲取路由器,分發(fā)器,插件和動作控制器。
具體使用方法:
class FooController extends Zend_Controller_Action { public function init() { $bootstrap = $this->getInvokeArg('bootstrap'); $view = $bootstrap->getResource('view'); // ... } }
為了防止重復(fù)注冊加載資源方法和插件或一些資源可能依賴于其他資源。為了解決這兩個問題,Zend_Application_Bootstrap_BootstrapAbstract提供了一個簡單的依賴性跟蹤機制。
如前所述,所有的資源 - 無論是方法或插件 - 是通過 bootstrap($resource)加載運行的,其中 $resource是資源的名稱,或者資源名稱數(shù)組,或者為空,為空表示加載運行所有資源。
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { protected function _initRequest() { // Ensure the front controller is initialized $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; } }
希望本文所述對大家PHP程序設(shè)計有所幫助。
更多Zend Framework教程之Application和Bootstrap用法詳解相關(guān)文章請關(guān)注PHP中文網(wǎng)!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
