当前位置: 移动技术网 > IT编程>开发语言>PHP > Zend Framework教程之Application和Bootstrap用法详解

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

滑头鬼之孙mp4,贺岁词,爱去小说网

本文实例讲述了zend framework教程之application和bootstrap用法。分享给大家供大家参考,具体如下:

在一个mvc应用程序中,我们需要初始化建立数据库链接,配置视图和视图助手,配置布局,注册相关插件,注册action 助手等等,这些配置和准备工作我们都需要一一完成。有时候可能有一些初始化操作需要,但是在有些情况下这些初始化可能不需要。通过zend_application不仅仅可以完成这些操作,而且可以让这些配置和初始化工作更统一有序,重用性更高。

zend_application使用可以细分成三种:

zend_application:加载php环境,包括include_paths和自动加载,并实例化引导类。

zend_application_bootstrap:提供引导类的接口。

zend_application_bootstrap_bootstrap完成大多数引导需要提供的通用功能,包括依赖性检查和按需加载引导资源。

zend_application_resource提供资源按需加载功能

开发人员可以根据需要继承zend_application_bootstrap_bootstrap或实现zend_application_bootstrap_bootstrapper接口。在入口文件(例如,public/index.php)加载zend_application,并根据引导选项和当前环境配置实例化。

引导选项包括指定的引导类文件和引导类路径,选项具体如下:

所需要的include_paths

自动加载功能额外加载注册的命名空间

php.ini初始化设置

指定bootstrap类名,如果不是"bootstrap"

资源的前缀键值对键表示资源前缀名称

资源的类名或者别名

附加加载的配置文件路径

附加的配置选项

选项可以是一个数组,或者zend_config对象,或者是指定位置的配置文件

引导程序

zend_application的第二个功能就是引导应用,bootstraps 必须实现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主要提供了环境配置,获取引导加载的资源,以及引导程序

你可以实现接口或者继承zend_application_bootstrap_bootstrapabstract,或者zend_application_bootstrap_bootstrap.

资源方法

实现zend_application_bootstrap_bootstrapabstract接口的资源方法必须遵循如下规则. 方法类型是protected,方法的前缀必须是_init will开头.

如果要加载使用一个资源方法,在bootstrap()中添加资源名称即可,资源名称是资源方法去掉_init前缀。

如果要加载使用多个资源方法,可以通过数组指定。

例如 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'));

加载使用全部资源方法,使用无参的bootstrap():

$bootstrap->bootstrap();

新建first_web项目

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 提供了一个可重用资源的引导,通用和模块化的引导类和依赖检查。 同时默认负责设置 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_”;任何以下,这都是资源的名称。例如,

$application = new zend_application(application_env, array(
  'resources' => array(
    'frontcontroller' => array(
      'controllerdirectory' => application_path . '/controllers',
    ),
  ),
));

"frontcontroller"资源是个特例。他的选项比较特殊。

无论是使用自己的写的资源插件还是使用第三方的资源插件。你必须保证bootstrap能找到他们,bootstrap 内部通过 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',
    ),
  ),
));

在应用程序中比可以使用指定目录下的资源。

和资源方法类似,通过使用 the bootstrap() 方法可以加载资源插件。加载单一,多个,全部的资源插件的配置方式也类似.

例如:

// execute one:
$bootstrap->bootstrap('frontcontroller');
// execute several:
$bootstrap->bootstrap(array('frontcontroller', 'foo'));
// execute all resource methods and plugins:
$bootstrap->bootstrap();

资源注册表

为了避免资源的重复注册,导致不必要的浪费zend_application_bootstrap_bootstrapabstract 提供了一个本地注册表对象存储这些资源对象.当你想要存放一个资源的时候,只需要在方法中返回这个资源即可。

为了灵活性,注册表是作为一个内部“容器”存在的。只要是对象都可以存入到容器中。资源名称对应为容器的属性。默认情况下,可以通过zend_registry获取实例使用,也可以自定义其他对象。 setcontainer() and getcontainer() 方法可用于操纵容器本身。getresource($resource) 可用于获取一个指定资源。hasresource($resource) 可以检查资源是否已经注册存在

例如,注册一个view资源:

class bootstrap extends zend_application_bootstrap_bootstrap
{
  protected function _initview()
  {
    $view = new zend_view();
    // more initialization...
    return $view;
  }
}

资源的相关操作:

// 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() , 执行了 run() 之后,它会注册为前端控制器参数的“bootstrap”,通过他可以获取路由器,分发器,插件和动作控制器。

具体使用方法:

class foocontroller extends zend_controller_action
{
  public function init()
  {
    $bootstrap = $this->getinvokearg('bootstrap');
    $view = $bootstrap->getresource('view');
    // ...
  }
}

为了防止重复注册加载资源方法和插件或一些资源可能依赖于其他资源。为了解决这两个问题,zend_application_bootstrap_bootstrapabstract提供了一个简单的依赖性跟踪机制。

如前所述,所有的资源 - 无论是方法或插件 - 是通过 bootstrap($resource)加载运行的,其中 $resource是资源的名称,或者资源名称数组,或者为空,为空表示加载运行所有资源。

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

更多关于zend相关内容感兴趣的读者可查看本站专题:《zend framework框架入门教程》、《php优秀开发框架总结》、《yii框架入门及常用技巧总结》、《thinkphp入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网