Home > Backend Development > PHP Tutorial > Detailed explanation of Application usage examples of Zend Framework tutorial, zendframework_PHP tutorial

Detailed explanation of Application usage examples of Zend Framework tutorial, zendframework_PHP tutorial

WBOY
Release: 2016-07-12 08:57:11
Original
1350 people have browsed it

Detailed explanation of application usage examples of Zend Framework tutorial, zendframework

This article explains the application usage of Zend Framework tutorial with examples. Share it with everyone for your reference, the details are as follows:

Zend_Application is the core component of Zend Framework. Zend_Application provides basic functionality for Zend Framework applications and is the entry point of the program. Its main functions are two: loading and configuring the PHP environment (including automatic loading), and booting the application.

Typically, the Zend_Application constructor is configured via configuration options, but it can also be configured entirely using a custom method. Below are two use cases.

Zend_Application configuration options

Constructor:

/**
 * 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);
  }
}

Copy after login

Zend_Application configuration method

1. Use configuration file
2. Use configuration array

Common configuration options

Option Description
phpSettings

Used to configure php.ini options, required It is an array, and the key of the array should be the key of the option.

Option Description
phpSettings

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

includePaths

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

autoloaderNamespaces

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

bootstrap

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

includePaths

Add additional paths to include_path, required is an array<🎜>

autoloaderNamespaces

Register an additional namespace to Zend_Loader_Autoloader as an array <🎜>

bootstrap

You can set the path of the bootstrap guide class String, or array. The array elements are required to be 'path' and 'class'<🎜>


Note:

Option names are not case-sensitive.

Zend_Application method

Method Return Value Parameters Description
__construct(
$environment,
$options = null)
Void
  • $environment: Required. A String representing the current application environment.

    Typical strings may include "development", "testing", "qa", or "production", which must already be defined.

    Corresponds to the relevant section in the configuration file.

  • $options: Optional, the parameter type may be :

    • String : Specifies the configuration path of the Zend_Config file. $environment is used to specify which section of the configuration file

      Starting from 1.10, multiple configuration file paths can be set, which will then be merged into a single configuration file.

      This is more flexible and easier to reuse.

      The key in this case is "config" and its value is an array of file paths.

      Note: It can be a path string, or array("config"=>array("/path1","/path2"[,...]));.

    • Array : Configure the application’s associative array

    • Zend_Config: Instance of configuration object

Constructor. Used to initialize configuration objects. Instantiate Zend_Loader_Autoloader.

By passing options to the constructor and then to the setOptions() method.

getEnvironment() String N/A

Get environment configuration

getAutoloader() Zend_Loader_Autoloader N/A

Get Zend_Loader_Autoloader instance

setOptions(array $options) Zend_Application
  • $options: Required, the requirement is an array

All options are stored inside a reference and the method is called multiple times to merge the options.

The setter method will be generated according to the options.

For example, the option "phpSettings" corresponds to setPhpSettings().

(Option names are not case sensitive.)

getOptions() Array N/A


hasOption($key) Boolean
  • $key: It is judged that the specified configuration is issued

key is not case sensitive.

getOption($key) Mixed
  • $key: Get the value of the specified configuration option

key is not case sensitive. If it does not exist, return NULL

setPhpSettings(array $settings, $prefix = '') Zend_Application
  • $settings: Compare .PHP Configuration association array of INI .

  • $prefix: Optional. Add prefix

    to the option


setAutoloaderNamespaces(array $namespaces) Zend_Application
  • $namespaces: Required.

    Pass an array of namespace strings, registered via the Zend_Loader_Autoloader instance


setBootstrap($path, $class = null) Zend_Application
  • $path: Required.

    Probably a Zend_Application_Bootstrap_Bootstrapper instance,

    Bootstrap class path string,

    Associative array in the format classname => filename,

    Or an associative array with key "class" and value "path".

  • $class: Optional. If $path is a string, $class is the class name

getBootstrap() NULL |Zend_Application_Bootstrap_Bootstrapper N/A

Get the registered bootstrap instance.

bootstrap() Void N/A

Call bootstrap's bootstrap() to boot the application.

run() Void N/A

Call bootstrap's run() to run the application

配置举例:

默认:

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

Copy after login

源代码

<&#63;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'])
              &#63; $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&#63;
   *
   * @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]))
                 &#63; $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) &#63; $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')
           &#63; 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();
  }
}

Copy after login

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

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

您可能感兴趣的文章:

  • Zend Framework自定义Helper类相关注意事项总结
  • Zend Framework教程之Bootstrap类用法概述
  • Zend Framework教程之资源(Resources)用法实例详解
  • Zend Framework教程之Application和Bootstrap用法详解
  • Zend Framework教程之配置文件application.ini解析
  • Zend Framework教程之Loader以及PluginLoader用法详解
  • Zend Framework教程之Autoloading用法详解
  • Zend Framework教程之Resource Autoloading用法实例
  • Zend Framework教程之MVC框架的Controller用法分析
  • Zend Framework教程之路由功能Zend_Controller_Router详解
  • Zend Framework教程之Zend_Controller_Plugin插件用法详解
  • Zend Framework教程之响应对象的封装Zend_Controller_Response实例详解
  • Zend Framework教程之动作的基类Zend_Controller_Action详解
  • Zend Framework教程之前端控制器Zend_Controller_Front用法详解

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1110089.htmlTechArticleZend Framework教程之Application用法实例详解,zendframework 本文实例讲述了Zend Framework教程之Application用法。分享给大家供大家参考,具体如下:...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template