欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Zend Framework教程之Application和Bootstrap用法詳解

 更新時(shí)間:2016年03月10日 11:54:51   作者:coder  
這篇文章主要介紹了Zend Framework教程之Application和Bootstrap用法,結(jié)合實(shí)例形式詳細(xì)分析了Application和Bootstrap的功能,使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了Zend Framework教程之Application和Bootstrap用法。分享給大家供大家參考,具體如下:

在一個(gè)MVC應(yīng)用程序中,我們需要初始化建立數(shù)據(jù)庫(kù)鏈接,配置視圖和視圖助手,配置布局,注冊(cè)相關(guān)插件,注冊(cè)action 助手等等,這些配置和準(zhǔn)備工作我們都需要一一完成。有時(shí)候可能有一些初始化操作需要,但是在有些情況下這些初始化可能不需要。通過(guò)Zend_Application不僅僅可以完成這些操作,而且可以讓這些配置和初始化工作更統(tǒng)一有序,重用性更高。

Zend_Application使用可以細(xì)分成三種:

Zend_Application:加載PHP環(huán)境,包括include_paths和自動(dòng)加載,并實(shí)例化引導(dǎo)類(lèi)。

Zend_Application_Bootstrap:提供引導(dǎo)類(lèi)的接口。

Zend_Application_Bootstrap_Bootstrap完成大多數(shù)引導(dǎo)需要提供的通用功能,包括依賴(lài)性檢查和按需加載引導(dǎo)資源。

Zend_Application_Resource提供資源按需加載功能

開(kāi)發(fā)人員可以根據(jù)需要繼承Zend_Application_Bootstrap_Bootstrap或?qū)崿F(xiàn)Zend_Application_Bootstrap_Bootstrapper接口。在入口文件(例如,public/index.php)加載Zend_Application,并根據(jù)引導(dǎo)選項(xiàng)和當(dāng)前環(huán)境配置實(shí)例化。

引導(dǎo)選項(xiàng)包括指定的引導(dǎo)類(lèi)文件和引導(dǎo)類(lèi)路徑,選項(xiàng)具體如下:

所需要的include_paths

自動(dòng)加載功能額外加載注冊(cè)的命名空間

php.ini初始化設(shè)置

指定bootstrap類(lèi)名,如果不是"Bootstrap"

資源的前綴鍵值對(duì)鍵表示資源前綴名稱(chēng)

資源的類(lèi)名或者別名

附加加載的配置文件路徑

附加的配置選項(xiàng)

選項(xiàng)可以是一個(gè)數(shù)組,或者Zend_Config對(duì)象,或者是指定位置的配置文件

引導(dǎo)程序

Zend_Application的第二個(gè)功能就是引導(dǎo)應(yīng)用,Bootstraps 必須實(shí)現(xiàn)Zend_Application_Bootstrap_Bootstrapper接口, 具體接口API如下:

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主要提供了環(huán)境配置,獲取引導(dǎo)加載的資源,以及引導(dǎo)程序

你可以實(shí)現(xiàn)接口或者繼承Zend_Application_Bootstrap_BootstrapAbstract,或者Zend_Application_Bootstrap_Bootstrap.

資源方法

實(shí)現(xiàn)Zend_Application_Bootstrap_BootstrapAbstract接口的資源方法必須遵循如下規(guī)則. 方法類(lèi)型是protected,方法的前綴必須是_init will開(kāi)頭.

如果要加載使用一個(gè)資源方法,在bootstrap()中添加資源名稱(chēng)即可,資源名稱(chēng)是資源方法去掉_init前綴。

如果要加載使用多個(gè)資源方法,可以通過(guò)數(shù)組指定。

例如 bootstrap class:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
  protected function _initFoo()
  {
    // ...
  }
  protected function _initBar()
  {
    // ...
  }
  protected function _initBaz()
  {
    // ...
  }
}

只加載使用_initFoo()  :

$bootstrap->bootstrap('foo');

加載使用 _initFoo() and _initBar() :

$bootstrap->bootstrap(array('foo', 'bar'));

加載使用全部資源方法,使用無(wú)參的bootstrap():

$bootstrap->bootstrap();

新建first_web項(xiàng)目

root@coder-671T-M:/mydev_src/zend_framework_learn/www# tree first_web/
first_web/
├── application
│   ├── Bootstrap.php
│   ├── configs
│   │   └── application.ini
│   ├── controllers
│   │   ├── ErrorController.php
│   │   └── IndexController.php
│   ├── models
│   └── views
│       ├── helpers
│       └── scripts
│           ├── error
│           │   └── error.phtml
│           └── index
│               └── index.phtml
├── docs
│   └── README.txt
├── library
├── public
│   └── index.php
└── tests
    ├── application
    │   └── controllers
    │       └── IndexControllerTest.php
    ├── bootstrap.php
    ├── library
    └── phpunit.xml
16 directories, 11 files

較新版本的zend framework引入了Zend_Application和Bootstrap。Zend_Application 提供了一個(gè)可重用資源的引導(dǎo),通用和模塊化的引導(dǎo)類(lèi)和依賴(lài)檢查。 同時(shí)默認(rèn)負(fù)責(zé)設(shè)置 PHP 環(huán)境變量和自動(dòng)加載功能。

默認(rèn)新建項(xiàng)目后,會(huì)給出如下的幾個(gè)文件:

1.項(xiàng)目的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.項(xiàng)目入口文件

├── 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)功能。

資源插件

資源插件只需要實(shí)現(xiàn)Zend_Application_Resource_Resource,或者,更簡(jiǎn)單的是,繼承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能夠識(shí)別資源插件,定義資源插件時(shí),需要實(shí)現(xiàn)Zend_Application_Bootstrap_ResourceBootstrapper. 接口定義了查找插件,注冊(cè)插件和加載插件的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ù)利用,同時(shí)讓bootstrap更簡(jiǎn)潔,如果要修改,新增資源也無(wú)需修改你的bootstrap。

通過(guò)實(shí)現(xiàn)Zend_Application_Bootstrap_BootstrapAbstract (被 Zend_Application_Bootstrap_Bootstrap 繼承) ,才可以使用定義的資源插件

通過(guò)將指定的選項(xiàng)傳遞到application object and/or bootstrap,來(lái)注冊(cè)使用資源插件。這些選項(xiàng)可能會(huì)從一個(gè)配置文件,或通過(guò)手動(dòng)指定。規(guī)則是選項(xiàng)​​必須是鍵值對(duì),鍵代表資源名稱(chēng)。資源名稱(chēng),是資源插件類(lèi)的類(lèi)前綴。例如,Zend框架自帶的資源類(lèi)前綴“Zend_Application_Resource_”;任何以下,這都是資源的名稱(chēng)。例如,

$application = new Zend_Application(APPLICATION_ENV, array(
  'resources' => array(
    'FrontController' => array(
      'controllerDirectory' => APPLICATION_PATH . '/controllers',
    ),
  ),
));

"FrontController"資源是個(gè)特例。他的選項(xiàng)比較特殊。

無(wú)論是使用自己的寫(xiě)的資源插件還是使用第三方的資源插件。你必須保證bootstrap能找到他們,bootstrap 內(nèi)部通過(guò) Zend_Loader_PluginLoader可以讓我們只需要指定資源插件的類(lèi)前綴,值為資源類(lèi)的類(lèi)路徑便可以輕松注冊(cè)資源插件。

例如,如果編寫(xiě)的資源插件存放在APPLICATION_PATH/resources/ 下,類(lèi)前綴為My_Resource. 可以使用如下方法注冊(cè)加載:

$application = new Zend_Application(APPLICATION_ENV, array(
  'pluginPaths' => array(
    'My_Resource' => APPLICATION_PATH . '/resources/',
  ),
  'resources' => array(
    'FrontController' => array(
      'controllerDirectory' => APPLICATION_PATH . '/controllers',
    ),
  ),
));

在應(yīng)用程序中比可以使用指定目錄下的資源。

和資源方法類(lèi)似,通過(guò)使用 the bootstrap() 方法可以加載資源插件。加載單一,多個(gè),全部的資源插件的配置方式也類(lèi)似.

例如:

// Execute one:
$bootstrap->bootstrap('FrontController');
// Execute several:
$bootstrap->bootstrap(array('FrontController', 'Foo'));
// Execute all resource methods and plugins:
$bootstrap->bootstrap();

資源注冊(cè)表

為了避免資源的重復(fù)注冊(cè),導(dǎo)致不必要的浪費(fèi)Zend_Application_Bootstrap_BootstrapAbstract 提供了一個(gè)本地注冊(cè)表對(duì)象存儲(chǔ)這些資源對(duì)象.當(dāng)你想要存放一個(gè)資源的時(shí)候,只需要在方法中返回這個(gè)資源即可。

為了靈活性,注冊(cè)表是作為一個(gè)內(nèi)部“容器”存在的。只要是對(duì)象都可以存入到容器中。資源名稱(chēng)對(duì)應(yīng)為容器的屬性。默認(rèn)情況下,可以通過(guò)Zend_Registry獲取實(shí)例使用,也可以自定義其他對(duì)象。 setContainer() and getContainer() 方法可用于操縱容器本身。getResource($resource) 可用于獲取一個(gè)指定資源。hasResource($resource) 可以檢查資源是否已經(jīng)注冊(cè)存在

例如,注冊(cè)一個(gè)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;
}

請(qǐng)注意:注冊(cè)表容器是不是全局的。這意味著你需要通過(guò)訪問(wèn)的bootstrap來(lái)獲取資源。 Zend_Application_Bootstrap_Bootstrap提供了 run() , 執(zhí)行了 run() 之后,它會(huì)注冊(cè)為前端控制器參數(shù)的“bootstrap”,通過(guò)他可以獲取路由器,分發(fā)器,插件和動(dòng)作控制器。

具體使用方法:

class FooController extends Zend_Controller_Action
{
  public function init()
  {
    $bootstrap = $this->getInvokeArg('bootstrap');
    $view = $bootstrap->getResource('view');
    // ...
  }
}

為了防止重復(fù)注冊(cè)加載資源方法和插件或一些資源可能依賴(lài)于其他資源。為了解決這兩個(gè)問(wèn)題,Zend_Application_Bootstrap_BootstrapAbstract提供了一個(gè)簡(jiǎn)單的依賴(lài)性跟蹤機(jī)制。

如前所述,所有的資源 - 無(wú)論是方法或插件 - 是通過(guò) bootstrap($resource)加載運(yùn)行的,其中 $resource是資源的名稱(chēng),或者資源名稱(chēng)數(shù)組,或者為空,為空表示加載運(yùn)行所有資源。

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;
  }
}

更多關(guān)于zend相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Zend FrameWork框架入門(mén)教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《Yii框架入門(mén)及常用技巧總結(jié)》、《ThinkPHP入門(mén)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論