Zend Framework Action Assistant (Zend_controller_action_helper) usage, zendhelper_php tutorial

Source: Internet
Author: User
Tags zend framework

Zend Framework Action Assistant (Zend_controller_action_helper) usage, zendhelper


This example describes the Zend Framework Action Helper (zend_controller_action_helper) usage. Share to everyone for your reference, as follows:

With helper mode, you can encapsulate some of the frequently used function modules, which can be used flexibly where needed, mainly in action.

There are two assistants in the Zend framework, the Action Helper (Zend_controller_action_helper) and the attempt Assistant (Zend_view_helper).

The action helper can add functions (runtime and/or On-demand functionality) to any Zend_controller_action derivative action Controller, in order to increase the function of the public action controller, Minimizing the need for derivative action controller classes.

The action helper is loaded when it needs to be called, and can be instantiated at the time of the request (bootstrap) or when the action controller is created (init ()).

Related documents involved

In the/library/zend/controller/action/

│exception.php
│helperbroker.php
│interface.php

├─helper
││abstract.php
││actionstack.php
││ajaxcontext.php
││autocompletedojo.php
││autocompletescriptaculous.php
││cache.php
││contextswitch.php
││flashmessenger.php
││json.php
││redirector.php
││url.php
││viewrenderer.php
││
│└─autocomplete
│abstract.php

└─helperbroker
prioritystack.php

the common action Assistants are :

Flashmessenger is used to process flash messenger sessions;
JSON is used to decode and send JSON responses;
Url used to create URLs;
Redirector provides another way to enable the program to redirect to internal or external pages;
Viewrenderer automatically completes the process of building the View object within the controller and rendering the view;
AutoComplete automatically responds to AJAX auto-completion;
Contextswitch and Ajaxcontext provide alternative response formats for your actions;
Cache implementation of cache related operations;
The actionstack is used to manipulate the action stack.

Several examples of how to use the hands

1. Pass the Gethelper () method of the $_helper member of the Zend_controller_action . Call Gethelper () directly and the name of the incoming helper.

$redirector = $this->_helper->gethelper (' redirector ');//$redirector->getname (); $redirector Gotosimple (' Index2 ');

2. Directly by accessing the _helper helper property corresponding to the helper object .

$redirector = $this->_helper->redirector;

Zend_controller_action_helperbroker

The Chinese name translation "Assistant broker", as the name implies, is an action assistant intermediary.

The second way to instantiate an action is through the Zend_controller_action_helperbroker Magic Method __get ().

Helper brokers are used to register helper objects and helper paths, and to get assistants and so on.

Zend_controller_action_helperbroker implementation and List of common methods

<?php/** * @see zend_controller_action_helperbroker_prioritystack */require_once ' zend/controller/action/  helperbroker/prioritystack.php ';/** * @see zend_loader */require_once ' zend/loader.php ';/** * @category Zend * @package Zend_controller * @subpackage zend_controller_action * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http ://www.zend.com) * @license HTTP://FRAMEWORK.ZEND.COM/LICENSE/NEW-BSD new BSD license */class Zend_controller_action_h elperbroker{/** * $_actioncontroller-actioncontroller Reference * * @var zend_controller_action * * protected  $_actioncontroller;  /** * @var zend_loader_pluginloader_interface */protected static $_pluginloader; /** * $_helpers-helper Array * * @var Zend_controller_action_helperbroker_prioritystack */protected static $_s  tack = null;   /** * Set Pluginloader for use with broker * * @param zend_loader_pluginloader_interface $loader * @return void */public static function SetpluginloAder ($loader) {if ((null!== $loader) && (! $loader instanceof Zend_loader_pluginloader_interface)) {req      Uire_once ' zend/controller/action/exception.php ';    throw new Zend_controller_action_exception (' Invalid plugin loader provided to Helperbroker ');  } Self::$_pluginloader = $loader;  }/** * Retrieve pluginloader * * @return Zend_loader_pluginloader */public static function Getpluginloader ()      {if (null = = = Self::$_pluginloader) {require_once ' zend/loader/pluginloader.php '; Self::$_pluginloader = new Zend_loader_pluginloader (Array (' zend_controller_action_helper ' = ' zend/controller/a    ction/helper/',));  } return Self::$_pluginloader; }/** * Addprefix ()-ADD repository of helpers by prefix * * @param string $prefix */static public function ad    Dprefix ($prefix) {$prefix = RTrim ($prefix, ' _ ');    $path = Str_replace (' _ ', Directory_separator, $prefix); Self::getpluginloader ()->addprefixpath ($PREfix, $path);   }/** * Addpath ()-Add path to repositories where action_helpers could is found. * * @param string $path * @param string $prefix Optional; Defaults to ' zend_controller_action_helper ' * @return void */static public function Addpath ($path, $prefix = ' Zend_c  Ontroller_action_helper ') {Self::getpluginloader ()->addprefixpath ($prefix, $path);    }/** * Addhelper ()-ADD Helper Objects * * @param zend_controller_action_helper_abstract $helper * @return void */static Public function Addhelper (zend_controller_action_helper_abstract $helper) {self::getstack ()->push ($h    Elper);  Return    }/** * Resethelpers () * * @return void */static public Function Resethelpers () {self::$_stack = null;  Return }/** * Retrieve or initialize a helper statically * * Retrieves a helper object statically, loading On-demand if T He helper * Does not already exist in the stack. Always returns a helper, unless * the helper Class CAnnot be found. * * @param string $name * @return Zend_controller_action_helper_abstract */public static function Getstatichelper (    $name) {$name = Self::_normalizehelpername ($name);    $stack = Self::getstack ();    if (!isset ($stack->{$name})) {Self::_loadhelper ($name);  } return $stack->{$name}; }/** * Getexistinghelper ()-Get helper by name * * Static method to retrieve helper object. Only retrieves helpers already * initialized with the broker (either via Addhelper () or On-demand loading * via Gethel   Per ()). * * Throws An exception if the referenced helper does not exist in the * stack;   Use {@link hashelper ()} to check if the helper is registered * prior to retrieving it.   * * @param string $name * @return zend_controller_action_helper_abstract * @throws zend_controller_action_exception    */public static function Getexistinghelper ($name) {$name = Self::_normalizehelpername ($name);    $stack = Self::getstack (); if (!isset ($stack->{$name}) {require_once ' zend/controller/action/exception.php '; throw new Zend_controller_action_exception (' Action helper '. $name.    ' Have not been registered with the helper broker ');  } return $stack->{$name}; }/** * Return all registered helpers as Helper = Object Pairs * * @return Array */public static function g  Etexistinghelpers () {return self::getstack ()->gethelpersbyname ();   }/** * is a particular helper loaded in the broker? * * @param string $name * @return Boolean */public static function Hashelper ($name) {$name = Self::_normalize    Helpername ($name);  Return Isset (Self::getstack ()->{$name}); }/** * Remove a particular helper from the broker * * @param string $name * @return Boolean */public static    function Removehelper ($name) {$name = Self::_normalizehelpername ($name);    $stack = Self::getstack ();    if (Isset ($stack->{$name})) {unset ($stack->{$name}); }   return false;   }/** * Lazy load the priority stack and return it * * @return Zend_controller_action_helperbroker_prioritystack */public static function Getstack () {if (Self::$_stack = = null) {Self::$_stack = new Zend_controller_action_h    Elperbroker_prioritystack ();  } return self::$_stack; }/** * Constructor * * @param zend_controller_action $actionController * @return void */Public function __co    NStruct (zend_controller_action $actionController) {$this->_actioncontroller = $actionController;      foreach (Self::getstack () as $helper) {$helper->setactioncontroller ($actionController);    $helper->init ();  }}/** * Notifypredispatch ()-Called by Action Controller Dispatch method * * @return void */Public function    Notifypredispatch () {foreach (Self::getstack () as $helper) {$helper->predispatch (); }}/** * Notifypostdispatch ()-Called by Action Controller Dispatch method * * @return VOID */Public Function Notifypostdispatch () {foreach (Self::getstack () as $helper) {$helper->postdispatch (    ); }}/** * Gethelper ()-Get helper by name * * @param string $name * @return Zend_controller_action_helper_abstr    ACT */Public Function Gethelper ($name) {$name = Self::_normalizehelpername ($name);    $stack = Self::getstack ();    if (!isset ($stack->{$name})) {Self::_loadhelper ($name);    } $helper = $stack->{$name};    $initialize = false;    if (null = = = ($actionController = $helper->getactioncontroller ())) {$initialize = true;    } elseif ($actionController!== $this->_actioncontroller) {$initialize = true;    } if ($initialize) {$helper->setactioncontroller ($this->_actioncontroller)->init ();  } return $helper; }/** * Method overloading * * @param string $method * @param array $args * @return Mixed * @throws Zend_cont Roller_action_exception if helper does not have a direct () method */Public Function __call ($method, $args) {$helper = $this->gethelper ($method);      if (!method_exists ($helper, ' direct ')) {require_once ' zend/controller/action/exception.php '; throw new Zend_controller_action_exception (' Helper '. $method.    ' does not support overloading via direct () ');  } return Call_user_func_array (Array ($helper, ' direct '), $args); }/** * Retrieve helper by name as Object property * * @param string $name * @return Zend_controller_action_helpe  R_abstract */Public Function __get ($name) {return $this->gethelper ($name); }/** * Normalize helper Name for lookups * * @param string $name * @return String */protected static Functio n _normalizehelpername ($name) {if (Strpos ($name, ' _ ')!== false) {$name = Str_replace (",", Ucwords (Str_repl    Ace (' _ ', ' ', $name) ');  } return Ucfirst ($name); }/** * Load a helper * * @param string $name * @return void */protectedstatic function _loadhelper ($name) {try {$class = Self::getpluginloader ()->load ($name);      } catch (Zend_loader_pluginloader_exception $e) {require_once ' zend/controller/action/exception.php '; throw new Zend_controller_action_exception (' Action Helper by name '. $name.    ' Not found ', 0, $e);    } $helper = new $class (); if (! $helper instanceof zend_controller_action_helper_abstract) {require_once ' zend/controller/action/exception.php      '; throw new Zend_controller_action_exception (' Helper name ' $name. ' Class '. $class.    ' is not of type zend_controller_action_helper_abstract ');  } self::getstack ()->push ($helper); }}

Common uses of helper brokers:

First, register an assistant

1.

Zend_controller_action_helperbroker::addhelper ($helper);

2. Use the Addprefix () method with a class prefix parameter to add the path to the custom helper class.
The prefix is required to follow the class naming conventions of the Zend Framework.

ADD helpers prefixed with my_action_helpers in My/action/helpers/zend_controller_action_helperbroker::addprefix (' My_action_helpers ');

3. Use the Addpath () method The first parameter is a directory, the second is the class prefix (default is ' Zend_controller_action_helper ').

Used to map its own class prefix to the specified directory.

ADD helpers prefixed with Helper in Plugins/helpers/zend_controller_action_helperbroker::addpath ('./plugins/ Helpers ',                       ' Helper ');

Second, whether the interpretation assistant exists

Use the Hashelper ($name) method to determine if an assistant exists in the helper broker, $name is the short name of the helper (minus the prefix):

Check if ' Redirector ' helper is registered with the Broker:if (' Zend_controller_action_helperbroker::hashelper (' Redirector ') {  echo ' redirector helper registered ';}

Getting an assistant from an assistant broker has two static methods: Getexistinghelper () and Getstatichelper (). Getexistinghelper () will get an assistant that throws an exception only if it has been previously called or has been explicitly registered with an assistant broker. Getstatichelper () is the same as Getexistinghelper (), but if you haven't registered the assistant stack yet, it will try to initialize the helper, Getstatichelper () is a good choice for getting the assistant you want to configure.

All two methods take one parameter, $name, which is the short name of the helper (minus the prefix).

Check if ' Redirector ' helper is registered with the broker, and Fetch:if (Zend_controller_action_helperbroker::hashelpe R (' redirector ')) {  $redirector =    zend_controller_action_helperbroker::getexistinghelper (' Redirector ');} Or, simply retrieve it, not worrying on whether or not it was//previously registered: $redirector =  Zend_control Ler_action_helperbroker::getstatichelper (' Redirector ');}

removehelper ($name) Delete an assistant in the assistant broker, $name is the short name of the assistant .

Conditionally remove the ' redirector ' helper from the Broker:if (' Zend_controller_action_helperbroker::hashelper (' Redirector ') {  zend_controller_action_helperbroker::removehelper (' Redirector ')}

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'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
    • Controller usage Analysis of MVC Framework for Zend Framework Tutorial

http://www.bkjia.com/PHPjc/1106902.html www.bkjia.com true http://www.bkjia.com/PHPjc/1106902.html techarticle Zend Framework Action Assistant (Zend_controller_action_helper) usage, Zendhelper This example describes Zend Framework Action Assistant (Zend_controller_ Action_helper) usage. Share to the big ...

  • 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.