Application and bootstrap usage of the Zend Framework tutorial, zendbootstrap_php Tutorial

Source: Internet
Author: User
Tags zend framework

Application and bootstrap usage of the Zend framework tutorial, Zendbootstrap


This example describes the application and bootstrap usages of the Zend Framework tutorial. Share to everyone for your reference, as follows:

In an MVC application, we need to initialize the database link, configure the View and view assistant, configure the layout, register the relevant plugins, register the Action Assistant, and so on, which we all need one by one to complete. Sometimes there may be some initialization operations required, but in some cases these initializations may not be required. Not only do these things through zend_application, but they also make these configurations and initialization work more uniform, more reusable.

Zend_application can be subdivided into three types:

Zend_application: Loads the PHP environment, including include_paths and auto-loading, and instantiates the boot class.

Zend_application_bootstrap: Provides the interface for the boot class.

Zend_application_bootstrap_bootstrap completes most of the common features that the boot needs to provide, including dependency checking and loading of boot resources on demand.

Zend_application_resource provides resource-on-demand loading capabilities

Developers can inherit zend_application_bootstrap_bootstrap or implement Zend_application_bootstrap_bootstrapper interfaces as needed. The zend_application is loaded in the portal file (for example, public/index.php) and instantiated according to the boot options and the current environment configuration.

The boot options include the specified boot class file and the boot classpath, with the following options:

The required include_paths

Auto-load function extra load registered namespaces

PHP.ini Initialization settings

Specify the Bootstrap class name, if not "Bootstrap"

The prefix key value pair key for the resource indicates the resource prefix name

The class name or alias of the resource

Configuration file path for additional loads

Additional configuration options

The option can be an array, or a Zend_config object, or a configuration file at the specified location

Boot program

Zend_application's second function is to boot the application, bootstraps must implement the Zend_application_bootstrap_bootstrapper interface, the specific interface API is as follows:

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

The API mainly provides the environment configuration, obtains the boot load the resources, as well as the boot program

You can implement interfaces or inherit Zend_application_bootstrap_bootstrapabstract, or Zend_application_bootstrap_bootstrap.

Resource methods

The resource method that implements the Zend_application_bootstrap_bootstrapabstract interface must follow the following rules. The method type is protected, and the prefix of the method must be _init will start.

If you want to load using a resource method, add the resource name in Bootstrap () and the resource name is the resource method minus the _init prefix.

If you want to load using multiple resource methods, you can specify through an array.

For example Bootstrap class:

Class Bootstrap extends zend_application_bootstrap_bootstrap{  protected function _initfoo ()  {    //...  }  protected function _initbar ()  {    //...  }  protected function _initbaz ()  {    //...  }}

Load only using _initfoo ():

$bootstrap->bootstrap (' foo ');

Load using _initfoo () and _initbar ():

$bootstrap->bootstrap (Array (' foo ', ' Bar '));

Load using the full resource method, using the bootstrap () with no parameters:

$bootstrap->bootstrap ();

New First_web Project

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
directories, files

The newer version of the Zend Framework introduces zend_application and Bootstrap. Zend_application provides a reusable resource for bootstrap, generic and modular boot classes and dependency checking. It is also the default responsibility to set the PHP environment variable and auto load function.

When you create a new project by default, several files are given:

1. Bootstrap of the project

first_web/
├──application
│├──bootstrap.php

The specific code is as follows:

<?phpclass Bootstrap extends zend_application_bootstrap_bootstrap{}

2. Configuration files

│├──configs
││└──application.ini

The specific code is as follows:

[production]phpsettings.display_startup_errors = 0phpsettings.display_errors = 0includepaths.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 = 1phpsettings.display_errors = 1[development:production]phpsettings.display_ Startup_errors = 1phpsettings.display_errors = 1resources.frontcontroller.params.displayexceptions = 1

3. Project entry Document

├──public
│└──index.php

<?php//Define Path to Application directorydefined (' Application_path ')  | | Define (' application_path ', Realpath (DirName (__file__). '/.. /application ');//Define Application environmentdefined (' application_env ')  | | Define (' application_env ', ( getenv (' application_env ')? getenv (' application_env '): ' production ');//Ensure library/is on Include_pathset_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 ();

The above code is the basic use of the Zend_application method, the completion of the initialization of the environment variables, loading configuration files, initializing the environment, loading modules, the completion of the Web application boot function.

Resource Plug-in

Resource plug-ins only need to implement Zend_application_resource_resource, or, more simply, inherit zend_application_resource_resourceabstract. The interface is as follows:

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

For example

Class My_resource_view extends zend_application_resource_resourceabstract{  protected $_view;  Public function init ()  {    //return view so bootstrap'll 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;  }}

Load using a resource plug-in

I did. Provides reuse of resources, you can define resource methods as resource plug-ins.

In order for Bootstrap to be able to identify the resource plug-in, it is necessary to implement zend_application_bootstrap_resourcebootstrapper when defining the resource plug-in. The interface defines the lookup plugin, registers the plugin and loads the plugin's 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 ();}

The use of resource plug-ins, not only can make resources can be reused, but also make bootstrap more concise, if you want to modify, new resources do not need to modify your bootstrap.

You can use a defined resource plug-in by implementing Zend_application_bootstrap_bootstrapabstract (inherited by Zend_application_bootstrap_bootstrap)

Registers the use of a resource plug-in by passing the specified options to the Application object and/or bootstrap. These options may be from a profile, or by manually specifying it. The rule is that the option must be a key-value pair and the key represents the resource name. The resource name, which is the class prefix for the resource plug-in class. For example, the Zend framework comes with the resource class prefix "Zend_application_resource_"; any of the following, this is the name of the resource. For example

$application = new Zend_application (application_env, Array ('  resources ' = = Array (    ' frontcontroller ' = Array (      ' controllerdirectory ' = = Application_path. '/controllers ',    ),)  ,));

The "Frontcontroller" resource is a special case. His options are quite special.

Either use your own Write resource plug-in or use a third-party resource plug-in. You have to make sure Bootstrap can find them, bootstrap. Internal through Zend_loader_pluginloader allows us to specify the class prefix of the resource plug-in, and the resource plug-in can be easily registered by the value of the class path of the resources class.

For example, if you write a resource plug-in that is stored under application_path/resources/, the class prefix is my_resource. You can register the load using the following methods:

$application = new Zend_application (application_env, Array (  ' pluginpaths ' = = Array (    ' my_resource ' = Application_path. '/resources/',  ),  ' resources ' = = Array ('    frontcontroller '      = = Array (' controllerdirectory ' = > Application_path. '/controllers ',    ),)  ,));

Resources in the specified directory can be used in the application.

Similar to resource methods, you can load a resource plug-in by using the bootstrap () method. Loading a single, multiple, full-resource plug-in is similarly configured.

For example:

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

Resource Registration Form

In order to avoid duplication of resources, resulting in unnecessary waste zend_application_bootstrap_bootstrapabstract Provides a local registry object to store these resource objects. When you want to store a resource, simply return the resource in the method.

For flexibility, the registry is present as an internal "container". As long as the object can be stored in the container. The resource name corresponds to the container's properties. By default, you can use Zend_registry to get instances, or you can customize other objects. The Setcontainer () and GetContainer () method can be used to manipulate the container itself. GetResource ($resource) can be used to get a specified resource. Hasresource ($resource) to check if a resource is already registered

For example, to register a view resource:

Class Bootstrap extends zend_application_bootstrap_bootstrap{  protected function _initview ()  {    $view = New Zend_view ();    More Initialization    ... return $view;  }}

Related Operations for resources:

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

Please note: The registry container is not global. This means that you need to access the bootstrap to get the resources. Zend_application_bootstrap_bootstrap provides run (), after executing run (), it registers as the "Bootstrap" of the front controller parameters, through which he can acquire routers, distributors, plugins and action controllers.

Specific Use method:

Class Foocontroller extends zend_controller_action{public  function init ()  {    $bootstrap = $this Getinvokearg (' bootstrap ');    $view = $bootstrap->getresource (' view ');    // ...  }}

To prevent duplicate registrations, load resource methods and plug-ins or some resources may depend on other resources. To solve these two problems, Zend_application_bootstrap_bootstrapabstract provides a simple dependency tracking mechanism.

As mentioned earlier, all resources-either methods or plug-ins-are run through Bootstrap ($resource), where $resource is the name of the resource, or an array of resource names, or empty, empty means that all resources are loaded to run.

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

More interested in Zend related content readers can view the topic: "Zend framework of the introductory tutorial", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Tutorial", "PHP object-oriented Programming introduction tutorial "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • Zend Framework Tutorial configuration file Application.ini parsing
    • Zend Framework Tutorial Loader and Pluginloader usage
    • Zend Framework Tutorial Autoloading Usage
    • Examples of resource autoloading usages of the Zend framework tutorial
    • Controller usage Analysis of MVC Framework for Zend Framework Tutorial
    • Zend Framework Tutorial's routing function zend_controller_router detailed
    • Zend Framework Tutorial Zend_controller_plugin plugin Usage
    • Package Zend_controller_response Example of response object for Zend Framework tutorial
    • Package Zend_controller_request example of request object for Zend Framework tutorial
    • Zend Framework Tutorial Action base class Zend_controller_action detailed
    • Zend Framework Tutorial Distributor Zend_controller_dispatcher Usage
    • Zend Framework Tutorial Front Controller Zend_controller_front Usage

http://www.bkjia.com/PHPjc/1108610.html www.bkjia.com true http://www.bkjia.com/PHPjc/1108610.html techarticle Zend Framework Tutorial application and bootstrap usage, Zendbootstrap This example describes Zend and application usage of the bootstrap framework tutorial. Share to everyone for your reference, ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.