Zend Framework Action Assistant (Zend_controller_action_helper) usage detailed _php example

Source: Internet
Author: User
Tags zend zend framework

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

With assistant mode, you can encapsulate some of the functional modules that are often used, so that you can use them flexibly where you need them, mainly in action.

There are two types of assistants in the Zend framework, the Action Assistant (Zend_controller_action_helper), and the Attempt Assistant (Zend_view_helper).

The Action assistant can be added to any Zend_controller_action derivative action Controller (runtime and/or On-demand functionality) in order to increase the functionality of the public action controller Minimize the need for derivative action controller classes.

The Action assistant loads when it needs to be invoked, either 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;
URLs are used to create URLs;
Redirector provides another way to implement the Help program to redirect to internal or external pages;
Viewrenderer automatically completes the process of establishing the view object and rendering the view within the controller;
AutoComplete automatic response to AJAX automatic completion;
Contextswitch and Ajaxcontext provide alternative response formats for your actions;
Cache implementation of cache related operations;
Actionstack is used to manipulate the action stack.

Several practical ways to use it

1. The Gethelper () method of $_helper members through Zend_controller_action . Call Gethelper () directly, and pass in the name of the assistant.

$redirector = $this->_helper->gethelper (' Redirector ');
$redirector->getname ();
$redirector->gotosimple (' index2 ');

2. The helper object that corresponds directly to the properties of the _helper assistant that is accessed .

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

Zend_controller_action_helperbroker

The Chinese name translated "assistant broker", as the name suggests, is the action Assistant middleman.

The second way in which the action is instantiated is through the Zend_controller_action_helperbroker Magic Method __get ().

The assistant broker is used to register helper objects and helper paths, and to get assistants, and so on.

Zend_controller_action_helperbroker implementations 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 Li  Cense/class Zend_controller_action_helperbroker {/** * $_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 Stati
  c $_stack = null; /** * Set Pluginloader for use with broker * * @param zend_loader_pluginloader_interface $loader * @return Voi
 D  */public static function Setpluginloader ($loader) {if (null!== $loader) && (! $loader instanceof Zend
      _loader_pluginloader_interface)) {require_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 Getpluginload
      ER () {if (null = = Self::$_pluginloader) {require_once ' zend/loader/pluginloader.php '; Self::$_pluginloader = new Zend_loader_pluginloader (Array (' Zend_controller_action_helper ' => ' Zend/Controller/
    action/helper/',));
  return self::$_pluginloader; /** * Addprefix ()-ADD repository of helpers by prefix * * @param string $prefix/static public funct
    Ion Addprefix ($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 = ' Zen
  D_controller_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 ()-&G
    T;push ($helper);
  Return }/** * Resethelpers () * * @return void */static public Function Resethelpers () {self::$_stack = n
    ull;
  Return }/** * Retrieve or initialize a helper statically * * Retrieves a helper object statically, loading On-demand If the 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 Getstatichel
    Per ($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 Geth
   Elper ()). * * Throws An exception if the referenced helper does not exist in the * stack;
   Use {@link hashelper ()} to check if the helper are registered * prior to retrieving it. * * @param string $name * @return zend_controller_action_helper_abstract * @throws Zend_controller_action_excepti
 On  * * 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.
    ' has not been registered with the helper broker ');
  return $stack->{$name}; /** * Return all registered Helpers as helper => object pairs * * @return array/public static Func
  tion Getexistinghelpers () {return self::getstack ()->gethelpersbyname ();
   }/** * is a particular helper loaded in the broker? * * @param string $name * @return Boolean/public static function Hashelper ($name) {$name = Self::_nor
    Malizehelpername ($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_prioritystac K/public static function Getstack () {if (Self::$_stack = = null) {Self::$_stack = new Zend_controller
    _action_helperbroker_prioritystack ();
  return self::$_stack; }/** * Constructor * * @param zend_controller_action $actionController * @return void/Public functi
    On __construct (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
    _abstract */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 Ze
  Nd_controller_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 * * @param string $name * @return Zend_controller_action_
  Helper_abstract */Public Function __get ($name) {return $this->gethelper ($name);
   /** * Normalize helper name for lookups * * @param string $name * @return string* * protected static function _normalizehelpername ($name) {if (Strpos ($name, ' _ ')!== false) {$name = Str_r
    Eplace (', ', ', Ucwords (Str_replace (' _ ', ', $name));
  Return Ucfirst ($name); /** * Load a helper * * @param string $name * @return void/protected static 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 usage of assistant brokers:

One, 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. Using the Addpath () method The first parameter is a directory, and 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 assistant broker, $name is the short name of the assistant (with the prefix removed):

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

There are two static methods for getting an assistant from an assistant broker: Getexistinghelper () and Getstatichelper (). Getexistinghelper () will get the assistant to throw an exception only if it was previously invoked or explicitly registered through an assistant broker. Getstatichelper () is the same as Getexistinghelper (), but if you haven't registered the helper stack, it will attempt to initialize the assistant, and Getstatichelper () is a good choice for getting the assistant you want to configure.

Each of the two methods takes a parameter $name, which is the short name of the assistant (remove the prefix).

Check if ' Redirector ' helper is registered with the broker, and fetch:
if (zend_controller_action_helperbroker::has Helper (' redirector ') {
  $redirector =
    zend_controller_action_helperbroker::getexistinghelper (' Redirector ');
}
Or, simply retrieve it, not worrying about whether or isn't it was
//previously registered:
$redirector =
   
    zend_controller_action_helperbroker::getstatichelper (' redirector ');
}


   

Removehelper ($name) deletes 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 site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

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.