Zend Framework教程之Application用法实例详解_php实例

本文实例讲述了Zend Framework教程之Application用法。分享给大家供大家参考,具体如下:

Zend_Application是Zend Framework的核心组件。Zend_Application为Zend Framework应用程序提供基本功能,是程序的入口点。它的主要功能有两个:装载配置PHP环境(包括自动加载),并引导应用程序。

通常情况下,通过配置选项配置Zend_Application构造器,但也可以完全使用自定义方法配置。以下是两个使用用例。

Zend_Application配置选项

构造函数:

/**
 * Constructor
 *
 * Initialize application. Potentially initializes include_paths, PHP
 * settings, and bootstrap class.
 *
 * @param string          $environment
 * @param string|array|Zend_Config $options String path to configuration file, or array/Zend_Config of configuration options
 * @throws Zend_Application_Exception When invalid options are provided
 * @return void
 */
public function __construct($environment, $options = null)
{
  $this->_environment = (string) $environment;
  require_once 'Zend/Loader/Autoloader.php';
  $this->_autoloader = Zend_Loader_Autoloader::getInstance();
  if (null !== $options) {
    if (is_string($options)) {
      $options = $this->_loadConfig($options);
    } elseif ($options instanceof Zend_Config) {
      $options = $options->toArray();
    } elseif (!is_array($options)) {
      throw new Zend_Application_Exception('Invalid options provided; must be location of config file, a config object, or an array');
    }
    $this->setOptions($options);
  }
}

Zend_Application配置方法

1.使用配置文件
2.使用配置数组

常见配置选项

Option Description
phpSettings
用于配置php.ini选项,要求是数组,数组的键应该是选项的的key.

includePaths
把附加的路径加入到include_path,要求是数组

autoloaderNamespaces
给Zend_Loader_Autoloader注册附加命名空间,为数组

bootstrap
可以是设置bootstrap引导类的路径的字符串,也可以是数组,数组元素要求为 'path' 和 'class'

注意:

选项名称不区分大小写。

Zend_Application的方法

Method Return Value Parameters Description
__construct(
$environment, 
$options = null)
Void
  • $environment: 必填。 表示当前应用环境的String。

    典型的字符串可能包括 "development", "testing", "qa", or "production",他们必须已经被定义。

    对应于配置文件文件中相关章节。

  • $options: 可选的,参数类型可能是:
    • String :  指定Zend_Config 文件的配置路径. $environment 用于指定配置文件的哪一个章节

      从1.10开始,可以设置多个配置文件路径,然后会被合并成一个单一的配置文件。

      这样更灵活,便于重用。

      在这种情况下的key是"config",其值是文件路径数组。

      注:可以是路径字符串,或 array("config"=>array("/path1","/path2"[,...]));.

    • Array : 配置应用的关联数组
    • Zend_Config:配置对象的实例

构造函数。 用于初始化配置对象。   实例化Zend_Loader_Autoloader。

通过传递给构造函数选项然后传递给setOptions()方法。

getEnvironment() String N/A
获取环境配置

getAutoloader() Zend_Loader_Autoloader N/A
获取Zend_Loader_Autoloader实例

setOptions(array $options) Zend_Application
  • $options: 必填,要求是数组

所有选项都存储在引用内部,并多次调用该方法来合并选项。

会根据选项生产对于的setter方法。 

例如,选项“phpSettings”对应setPhpSettings()。 

(选项名称不区分大小写。)

getOptions() Array N/A
hasOption($key) Boolean
  • $key: 判断是发有指定的配置

key不区分大小写。

getOption($key) Mixed
  • $key: 获取指定的配置选项的值

key不区分大小写。如果不存在返回 NULL

setPhpSettings(array $settings, $prefix = '') Zend_Application
  • $settings: 比填.PHP INI 的配置关联数组.
  • $prefix: 可选. 为选项添加前缀
setAutoloaderNamespaces(array $namespaces) Zend_Application
  • $namespaces: 必填

    传递命名空间字符串数组,通过Zend_Loader_Autoloader实例注册

setBootstrap($path, $class = null) Zend_Application
  • $path: 必填

    可能是Zend_Application_Bootstrap_Bootstrapper实例,

    自举类路径字符串, 

    格式为classname => filename的关联数组,

    或key为“class”和value为“path”的关联数组。

  • $class: 可选. 如果 $path 是字符串, $class 类名称
getBootstrap() NULL |Zend_Application_Bootstrap_Bootstrapper N/A
获取注册的bootstrap实例.

bootstrap() Void N/A
调用 bootstrap的bootstrap() 引导应用.

run() Void N/A
调用bootstrap的 run()运行应用

配置举例:

默认:

// Create application, bootstrap, and run
$application = new Zend_Application(
  APPLICATION_ENV,
  APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
      ->run();

源代码

<?php
class Zend_Application
{  /**
   * Constructor
   *
   * Initialize application. Potentially initializes include_paths, PHP
   * settings, and bootstrap class.
   *
   * @param string          $environment
   * @param string|array|Zend_Config $options String path to configuration file, or array/Zend_Config of configuration options
   * @throws Zend_Application_Exception When invalid options are provided
   * @return void
   */
  public function __construct($environment, $options = null)
  {
    $this->_environment = (string) $environment;
    require_once 'Zend/Loader/Autoloader.php';
    $this->_autoloader = Zend_Loader_Autoloader::getInstance();
    if (null !== $options) {
      if (is_string($options)) {
        $options = $this->_loadConfig($options);
      } elseif ($options instanceof Zend_Config) {
        $options = $options->toArray();
      } elseif (!is_array($options)) {
        throw new Zend_Application_Exception('Invalid options provided; must be location of config file, a config object, or an array');
      }
      $this->setOptions($options);
    }
  }
  /**
   * Retrieve current environment
   *
   * @return string
   */
  public function getEnvironment()
  {
    return $this->_environment;
  }
  /**
   * Retrieve autoloader instance
   *
   * @return Zend_Loader_Autoloader
   */
  public function getAutoloader()
  {
    return $this->_autoloader;
  }
  /**
   * Set application options
   *
   * @param array $options
   * @throws Zend_Application_Exception When no bootstrap path is provided
   * @throws Zend_Application_Exception When invalid bootstrap information are provided
   * @return Zend_Application
   */
  public function setOptions(array $options)
  {
    if (!empty($options['config'])) {
      if (is_array($options['config'])) {
        $_options = array();
        foreach ($options['config'] as $tmp) {
          $_options = $this->mergeOptions($_options, $this->_loadConfig($tmp));
        }
        $options = $this->mergeOptions($_options, $options);
      } else {
        $options = $this->mergeOptions($this->_loadConfig($options['config']), $options);
      }
    }
    $this->_options = $options;
    $options = array_change_key_case($options, CASE_LOWER);
    $this->_optionKeys = array_keys($options);
    if (!empty($options['phpsettings'])) {
      $this->setPhpSettings($options['phpsettings']);
    }
    if (!empty($options['includepaths'])) {
      $this->setIncludePaths($options['includepaths']);
    }
    if (!empty($options['autoloadernamespaces'])) {
      $this->setAutoloaderNamespaces($options['autoloadernamespaces']);
    }
    if (!empty($options['autoloaderzfpath'])) {
      $autoloader = $this->getAutoloader();
      if (method_exists($autoloader, 'setZfPath')) {
        $zfPath  = $options['autoloaderzfpath'];
        $zfVersion = !empty($options['autoloaderzfversion'])
              ? $options['autoloaderzfversion']
              : 'latest';
        $autoloader->setZfPath($zfPath, $zfVersion);
      }
    }
    if (!empty($options['bootstrap'])) {
      $bootstrap = $options['bootstrap'];
      if (is_string($bootstrap)) {
        $this->setBootstrap($bootstrap);
      } elseif (is_array($bootstrap)) {
        if (empty($bootstrap['path'])) {
          throw new Zend_Application_Exception('No bootstrap path provided');
        }
        $path = $bootstrap['path'];
        $class = null;
        if (!empty($bootstrap['class'])) {
          $class = $bootstrap['class'];
        }
        $this->setBootstrap($path, $class);
      } else {
        throw new Zend_Application_Exception('Invalid bootstrap information provided');
      }
    }
    return $this;
  }
  /**
   * Retrieve application options (for caching)
   *
   * @return array
   */
  public function getOptions()
  {
    return $this->_options;
  }
  /**
   * Is an option present?
   *
   * @param string $key
   * @return bool
   */
  public function hasOption($key)
  {
    return in_array(strtolower($key), $this->_optionKeys);
  }
  /**
   * Retrieve a single option
   *
   * @param string $key
   * @return mixed
   */
  public function getOption($key)
  {
  }
  /**
   * Merge options recursively
   *
   * @param array $array1
   * @param mixed $array2
   * @return array
   */
  public function mergeOptions(array $array1, $array2 = null)
  {
    if (is_array($array2)) {
      foreach ($array2 as $key => $val) {
        if (is_array($array2[$key])) {
          $array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))
                 ? $this->mergeOptions($array1[$key], $array2[$key])
                 : $array2[$key];
        } else {
          $array1[$key] = $val;
        }
      }
    }
    return $array1;
  }
  /**
   * Set PHP configuration settings
   *
   * @param array $settings
   * @param string $prefix Key prefix to prepend to array values (used to map . separated INI values)
   * @return Zend_Application
   */
  public function setPhpSettings(array $settings, $prefix = '')
  {
    foreach ($settings as $key => $value) {
      $key = empty($prefix) ? $key : $prefix . $key;
      if (is_scalar($value)) {
        ini_set($key, $value);
      } elseif (is_array($value)) {
        $this->setPhpSettings($value, $key . '.');
      }
    }
    return $this;
  }
  /**
   * Set include path
   *
   * @param array $paths
   * @return Zend_Application
   */
  public function setIncludePaths(array $paths)
  {
    $path = implode(PATH_SEPARATOR, $paths);
    set_include_path($path . PATH_SEPARATOR . get_include_path());
    return $this;
  }
  /**
   * Set autoloader namespaces
   *
   * @param array $namespaces
   * @return Zend_Application
   */
  public function setAutoloaderNamespaces(array $namespaces)
  {
    $autoloader = $this->getAutoloader();
    foreach ($namespaces as $namespace) {
      $autoloader->registerNamespace($namespace);
    }
    return $this;
  }
  /**
   * Set bootstrap path/class
   *
   * @param string $path
   * @param string $class
   * @return Zend_Application
   */
  public function setBootstrap($path, $class = null)
  {
    // setOptions() can potentially send a null value; specify default
    // here
    if (null === $class) {
      $class = 'Bootstrap';
    }
    if (!class_exists($class, false)) {
      require_once $path;
      if (!class_exists($class, false)) {
        throw new Zend_Application_Exception('Bootstrap class not found');
      }
    }
    $this->_bootstrap = new $class($this);
    if (!$this->_bootstrap instanceof Zend_Application_Bootstrap_Bootstrapper) {
      throw new Zend_Application_Exception('Bootstrap class does not implement Zend_Application_Bootstrap_Bootstrapper');
    }
    return $this;
  }
  /**
   * Get bootstrap object
   *
   * @return Zend_Application_Bootstrap_BootstrapAbstract
   */
  public function getBootstrap()
  {
    if (null === $this->_bootstrap) {
      $this->_bootstrap = new Zend_Application_Bootstrap_Bootstrap($this);
    }
    return $this->_bootstrap;
  }
  /**
   * Bootstrap application
   *
   * @param null|string|array $resource
   * @return Zend_Application
   */
  public function bootstrap($resource = null)
  {
    $this->getBootstrap()->bootstrap($resource);
    return $this;
  }
  /**
   * Run the application
   *
   * @return void
   */
  public function run()
  {
    $this->getBootstrap()->run();
  }
  /**
   * Load configuration file of options
   *
   * @param string $file
   * @throws Zend_Application_Exception When invalid configuration file is provided
   * @return array
   */
  protected function _loadConfig($file)
  {
    $environment = $this->getEnvironment();
    $suffix   = pathinfo($file, PATHINFO_EXTENSION);
    $suffix   = ($suffix === 'dist')
           ? pathinfo(basename($file, ".$suffix"), PATHINFO_EXTENSION)
           : $suffix;
    switch (strtolower($suffix)) {
      case 'ini':
        $config = new Zend_Config_Ini($file, $environment);
        break;
      case 'xml':
        $config = new Zend_Config_Xml($file, $environment);
        break;
      case 'json':
        $config = new Zend_Config_Json($file, $environment);
        break;
      case 'yaml':
      case 'yml':
        $config = new Zend_Config_Yaml($file, $environment);
        break;
      case 'php':
      case 'inc':
        $config = include $file;
        if (!is_array($config)) {
          throw new Zend_Application_Exception('Invalid configuration file provided; PHP file does not return array value');
        }
        return $config;
        break;
      default:
        throw new Zend_Application_Exception('Invalid configuration file provided; unknown config type');
    }
    return $config->toArray();
  }
}

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

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

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索framework
, application
zend
zend framework实例、zend application、zend framework、zend framework 教程、zend framework 2,以便于您获取更多的相关知识。

时间: 2024-08-31 13:13:41

Zend Framework教程之Application用法实例详解_php实例的相关文章

Zend Framework教程之Zend_Layout布局助手详解_php实例

本文实例讲述了Zend Framework教程之Zend_Layout布局助手.分享给大家供大家参考,具体如下: 一.作用 布局的作用和模版的作用类似.可以认为是把网站通用.公共的部分拿出来作为通用的页面框架.例如一个基本的web页面,可能页面的头和尾都是一样,不一样的可能只是内容body部分不一样,可以把公共的部分做成模版.不仅可以提高开发效率,也为后期的维护带来方便. 二.使用 这里举一个简单的例子. 首先用zend studio创建一个基本的zend framework项目:layout_

Zend Framework教程之路由功能Zend_Controller_Router详解_php实例

本文实例讲述了Zend Framework教程之路由功能Zend_Controller_Router用法.分享给大家供大家参考,具体如下: Zend Framework的路由提供了两个主要功能路由和创建路由. Zend_Controller_Router的Route类和相应Route目录下的类定义常见的路由操作. 接口Zend_Controller_Router_Interface,类Zend_Controller_Router_Abstract和Zend_Controller_Router_R

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

本文实例讲述了Zend Framework教程之Application和Bootstrap用法.分享给大家供大家参考,具体如下: 在一个MVC应用程序中,我们需要初始化建立数据库链接,配置视图和视图助手,配置布局,注册相关插件,注册action 助手等等,这些配置和准备工作我们都需要一一完成.有时候可能有一些初始化操作需要,但是在有些情况下这些初始化可能不需要.通过Zend_Application不仅仅可以完成这些操作,而且可以让这些配置和初始化工作更统一有序,重用性更高. Zend_Appli

Zend Framework教程之Autoloading用法详解_php实例

本文实例讲述了Zend Framework教程之Autoloading用法.分享给大家供大家参考,具体如下: 一.概述 自动加载是一种机制,无需依赖手动编写PHP代码.参考»PHP手册自动加载,一旦自动加载器被定义,你试图使用一个没有定义的类或接口的情况下,它会自动被调用. 使用自动加载,在项目中你不必担心类的存放位置.定义一个良好定义的自动加载器,您不需要考虑一个类文件相对于当前类文件的位置,您只需使用类,自动加载器将自动查找文件. 此外,自动加载,确保只加载一次,提升了性能 -所以可以用它替

Zend Framework教程之Zend_Config_Ini用法分析_php实例

本文实例讲述了Zend Framework教程之Zend_Config_Ini用法.分享给大家供大家参考,具体如下: Zend_Config_Ini允许开发者通过嵌套的对象属性语法在应用程序中用熟悉的 INI 格式存储和读取配置数据.INI 格式在提供拥有配置数据键的等级结构和配置数据节之间的继承能力方面具有专长.配置数据等级结构通过用点或者句号 (.)分离键值.一个节可以扩展或者通过在节的名称之后带一个冒号(:)和被继承的配置数据的节的名称来从另一个节继承. parse_ini_file Ze

Zend Framework教程之Zend_Db_Table_Row用法实例分析_php实例

本文实例讲述了Zend Framework教程之Zend_Db_Table_Row用法.分享给大家供大家参考,具体如下: 1. 简介 Zend_Db_Table_Row是Zend Framework的行数据网关.通常来说,你不可以自己实例化Zend_Db_Table_Row, 而是通过调用Zend_Db_Table::find()方法或者Zend_Db_Table::fetchRow()方法将Zend_Db_Table_Row作为 结果数据返回过来.一旦你得到来一个Zend_Db_Table_R

Zend Framework教程之Zend_Db_Table用法详解_php实例

本文实例讲述了Zend_Db_Table用法.分享给大家供大家参考,具体如下: 1. 简介 Zend_Db_Table 是Zend Framework的表模块.它通过zend_db_adapter连接到 数据库,为数据库模式检查表对象,并对该表进行操作和查询. 2. 开始 首先需要为抽象类zend_db_table(ares注:该类为抽象类,所以不能直接实例 化,只能先继承该类,然后实例化子类)设定一个默认对数据库adapter;除非你 指定其他类型数据库adapter,否则,所有的zend_d

Zend Framework+smarty用法实例详解_php实例

本文实例讲述了Zend Framework+smarty用法.分享给大家供大家参考,具体如下: 一.Zend Framework简介 Zend Framework使用模型-视图-控制器(Model-View-Controller(MVC))结构.这个用来把你的程序分离成不同部分使得开发和维护变得容易. 运行Zend Framework需要:PHP 5.1.4 (或更高) .Web 服务器支持 mod_rewrite功能,本实例采用Apache. 从这里http://framework.zend.

Zend Framework路由器用法实例详解_php实例

本文实例讲述了Zend Framework路由器用法.分享给大家供大家参考,具体如下: 路由是一个过程,在这个过程中它去除URI的端点(跟着基本URL的URI部分),并将其分解成参数来决定哪个模块.哪个控制器和哪个动作应该接受请求. 模块.控制器.动作.及其参数被打包到Zend_Controller_Request_Http对象. 使用路由器 为了正确使用路由器,必须对其进行初始化操作. 创建路由器可以通过前端控制器实例的getRouter()方法来实现.该方法不需要任何参数,执行该方法可以返回