Home php教程 PHP开发 Detailed explanation of Application usage examples of Zend Framework tutorial

Detailed explanation of Application usage examples of Zend Framework tutorial

Dec 27, 2016 pm 02:19 PM
zend framework

The example in this article describes the application usage of Zend Framework tutorial. 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.

Normally, the Zend_Application constructor is configured through 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

Detailed explanation of Application usage examples of Zend Framework tutorial

Note:

Option names are not case-sensitive.

Methods of Zend_Application

Detailed explanation of Application usage examples of Zend Framework tutorial

Detailed explanation of Application usage examples of Zend Framework tutorial

Detailed explanation of Application usage examples of Zend Framework tutorial

##Configuration example:

Default:

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

Source code

<?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 &#39;Zend/Loader/Autoloader.php&#39;;
    $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(&#39;Invalid options provided; must be location of config file, a config object, or an array&#39;);
      }
      $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[&#39;config&#39;])) {
      if (is_array($options[&#39;config&#39;])) {
        $_options = array();
        foreach ($options[&#39;config&#39;] as $tmp) {
          $_options = $this->mergeOptions($_options, $this->_loadConfig($tmp));
        }
        $options = $this->mergeOptions($_options, $options);
      } else {
        $options = $this->mergeOptions($this->_loadConfig($options[&#39;config&#39;]), $options);
      }
    }
    $this->_options = $options;
    $options = array_change_key_case($options, CASE_LOWER);
    $this->_optionKeys = array_keys($options);
    if (!empty($options[&#39;phpsettings&#39;])) {
      $this->setPhpSettings($options[&#39;phpsettings&#39;]);
    }
    if (!empty($options[&#39;includepaths&#39;])) {
      $this->setIncludePaths($options[&#39;includepaths&#39;]);
    }
    if (!empty($options[&#39;autoloadernamespaces&#39;])) {
      $this->setAutoloaderNamespaces($options[&#39;autoloadernamespaces&#39;]);
    }
    if (!empty($options[&#39;autoloaderzfpath&#39;])) {
      $autoloader = $this->getAutoloader();
      if (method_exists($autoloader, &#39;setZfPath&#39;)) {
        $zfPath  = $options[&#39;autoloaderzfpath&#39;];
        $zfVersion = !empty($options[&#39;autoloaderzfversion&#39;])
              ? $options[&#39;autoloaderzfversion&#39;]
              : &#39;latest&#39;;
        $autoloader->setZfPath($zfPath, $zfVersion);
      }
    }
    if (!empty($options[&#39;bootstrap&#39;])) {
      $bootstrap = $options[&#39;bootstrap&#39;];
      if (is_string($bootstrap)) {
        $this->setBootstrap($bootstrap);
      } elseif (is_array($bootstrap)) {
        if (empty($bootstrap[&#39;path&#39;])) {
          throw new Zend_Application_Exception(&#39;No bootstrap path provided&#39;);
        }
        $path = $bootstrap[&#39;path&#39;];
        $class = null;
        if (!empty($bootstrap[&#39;class&#39;])) {
          $class = $bootstrap[&#39;class&#39;];
        }
        $this->setBootstrap($path, $class);
      } else {
        throw new Zend_Application_Exception(&#39;Invalid bootstrap information provided&#39;);
      }
    }
    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 = &#39;&#39;)
  {
    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 . &#39;.&#39;);
      }
    }
    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 = &#39;Bootstrap&#39;;
    }
    if (!class_exists($class, false)) {
      require_once $path;
      if (!class_exists($class, false)) {
        throw new Zend_Application_Exception(&#39;Bootstrap class not found&#39;);
      }
    }
    $this->_bootstrap = new $class($this);
    if (!$this->_bootstrap instanceof Zend_Application_Bootstrap_Bootstrapper) {
      throw new Zend_Application_Exception(&#39;Bootstrap class does not implement Zend_Application_Bootstrap_Bootstrapper&#39;);
    }
    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 === &#39;dist&#39;)
           ? pathinfo(basename($file, ".$suffix"), PATHINFO_EXTENSION)
           : $suffix;
    switch (strtolower($suffix)) {
      case &#39;ini&#39;:
        $config = new Zend_Config_Ini($file, $environment);
        break;
      case &#39;xml&#39;:
        $config = new Zend_Config_Xml($file, $environment);
        break;
      case &#39;json&#39;:
        $config = new Zend_Config_Json($file, $environment);
        break;
      case &#39;yaml&#39;:
      case &#39;yml&#39;:
        $config = new Zend_Config_Yaml($file, $environment);
        break;
      case &#39;php&#39;:
      case &#39;inc&#39;:
        $config = include $file;
        if (!is_array($config)) {
          throw new Zend_Application_Exception(&#39;Invalid configuration file provided; PHP file does not return array value&#39;);
        }
        return $config;
        break;
      default:
        throw new Zend_Application_Exception(&#39;Invalid configuration file provided; unknown config type&#39;);
    }
    return $config->toArray();
  }
}
Copy after login
I hope this article will be helpful to everyone in PHP programming.

For more detailed explanations of Application usage examples in the Zend Framework tutorial, please pay attention to the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
Using Zend Framework with PHP: Quick Start Guide Using Zend Framework with PHP: Quick Start Guide Jun 21, 2023 am 08:58 AM

Using ZendFramework with PHP: Quick Start Guide ZendFramework is an open source, PHP-based web application framework that is powerful and easily extensible. ZendFramework contains many useful components that can help you build efficient web applications. This article will introduce how to use ZendFramework in PHP to help you get started quickly. Install ZendFramewo

Efficient database query through Zend Framework middleware Efficient database query through Zend Framework middleware Jul 28, 2023 pm 01:13 PM

Implementing efficient database queries through ZendFramework middleware Introduction In the development process, database queries are an inevitable part. An efficient database query can greatly improve system performance and user experience. ZendFramework is a widely used PHP framework with powerful database operation functions. This article will introduce how to implement efficient database queries through ZendFramework middleware and provide corresponding code examples. 1. Understanding ZendF

Zend Framework middleware: implementing full-text search and paging functions Zend Framework middleware: implementing full-text search and paging functions Jul 30, 2023 pm 08:49 PM

ZendFramework is a powerful development framework that helps developers quickly build high-performance, scalable PHP applications. Among them, middleware is an important concept in ZendFramework, which can help us implement full-text search and paging functions. This article will introduce how to use middleware in ZendFramework to implement these two functions and provide code examples. 1. Full-text search function Full-text search is one of the common functions in modern applications.

Zend Framework middleware: Adds OAuth and OpenID login support to applications Zend Framework middleware: Adds OAuth and OpenID login support to applications Jul 28, 2023 pm 01:09 PM

ZendFramework Middleware: Adding OAuth and OpenID Login Support to Applications User authentication is a critical feature in today's Internet applications. In order to provide better user experience and security, many applications choose to integrate third-party login services, such as OAuth and OpenID. In ZendFramework, we can easily add OAuth and OpenID login support to our applications through middleware. First, we need to install Ze

Zend Framework middleware: Provides email notification and message push functions Zend Framework middleware: Provides email notification and message push functions Jul 29, 2023 pm 08:29 PM

ZendFramework middleware: Provides email notification and message push functions Introduction: With the development of the Internet and the popularity of smartphones, email notification and message push have become one of the commonly used functions in modern software development. In ZendFramework, we can use middleware to implement email notification and message push functions. This article will introduce how to use ZendFramework middleware to implement email notifications and message push, and provide corresponding code examples. 1. Preparation work in

Zend Framework middleware: Add social login functionality to web applications Zend Framework middleware: Add social login functionality to web applications Jul 28, 2023 pm 07:21 PM

ZendFramework is an open source framework based on PHP that provides many powerful tools and components for building scalable web applications. This article will introduce how to use ZendFramework's middleware to add social login functionality to web applications. Middleware is code that is executed before or after a request enters your application. It allows developers to customize and extend the process of handling requests. ZendFramework provides a flexible way to

CodeIgniter vs Zend Framework: Which framework is better for developing ERP systems? CodeIgniter vs Zend Framework: Which framework is better for developing ERP systems? Jun 19, 2023 am 08:53 AM

When you decide to develop an ERP system, choosing a suitable framework is crucial. Here we will compare the two PHP frameworks CodeIgniter and ZendFramework to help you find a framework that is more suitable for your ERP system development. CodeIgniter and ZendFramework are popular PHP frameworks. They all provide many features and are extensible and maintainable. However, these two frameworks differ significantly in some aspects and are more suitable for some applications.

Zend Framework middleware: Add Alipay and WeChat payment functions to applications Zend Framework middleware: Add Alipay and WeChat payment functions to applications Jul 28, 2023 pm 08:01 PM

ZendFramework middleware: Adding Alipay and WeChat payment functions to applications Introduction: With the popularity of mobile payments, Alipay and WeChat payment have become essential payment methods in many applications. This article will introduce how to use ZendFramework middleware to add Alipay and WeChat payment functions to the application. By studying this article, you will learn how to use middleware to simplify the payment process and apply it to your actual projects. 1. Preparation Before starting, you

See all articles