Create your own action assistant for ZendFramework. zendframework_PHP tutorial

Source: Internet
Author: User
Tags autoloader
ZendFramework creates its own action assistant, zendframework. ZendFramework creates its own action assistant. This article describes how zendframework creates its own action assistant. For your reference, see Zend Framework for creating your own action assistant.

This example describes how to create an action assistant for Zend Framework. We will share this with you for your reference. The details are as follows:

The abstract base class of the assistant is Zend_Controller_Action_Helper_Abstract. to define your own assistant, you must inherit this class.

The source code of the class is as follows:

<?php/** * @see Zend_Controller_Action */require_once 'Zend/Controller/Action.php';abstract class Zend_Controller_Action_Helper_Abstract{  /**   * $_actionController   *   * @var Zend_Controller_Action $_actionController   */  protected $_actionController = null;  /**   * @var mixed $_frontController   */  protected $_frontController = null;  /**   * setActionController()   *   * @param Zend_Controller_Action $actionController   * @return Zend_Controller_ActionHelper_Abstract Provides a fluent interface   */  public function setActionController(Zend_Controller_Action $actionController = null)  {    $this->_actionController = $actionController;    return $this;  }  /**   * Retrieve current action controller   *   * @return Zend_Controller_Action   */  public function getActionController()  {    return $this->_actionController;  }  /**   * Retrieve front controller instance   *   * @return Zend_Controller_Front   */  public function getFrontController()  {    return Zend_Controller_Front::getInstance();  }  /**   * Hook into action controller initialization   *   * @return void   */  public function init()  {  }  /**   * Hook into action controller preDispatch() workflow   *   * @return void   */  public function preDispatch()  {  }  /**   * Hook into action controller postDispatch() workflow   *   * @return void   */  public function postDispatch()  {  }  /**   * getRequest() -   *   * @return Zend_Controller_Request_Abstract $request   */  public function getRequest()  {    $controller = $this->getActionController();    if (null === $controller) {      $controller = $this->getFrontController();    }    return $controller->getRequest();  }  /**   * getResponse() -   *   * @return Zend_Controller_Response_Abstract $response   */  public function getResponse()  {    $controller = $this->getActionController();    if (null === $controller) {      $controller = $this->getFrontController();    }    return $controller->getResponse();  }  /**   * getName()   *   * @return string   */  public function getName()  {    $fullClassName = get_class($this);    if (strpos($fullClassName, '_') !== false) {      $helperName = strrchr($fullClassName, '_');      return ltrim($helperName, '_');    } elseif (strpos($fullClassName, '\\') !== false) {      $helperName = strrchr($fullClassName, '\\');      return ltrim($helperName, '\\');    } else {      return $fullClassName;    }  }}

The basic helper class provides the following common methods:

SetActionController () is used to set the current action controller.
Init (). This method is triggered by the assistant broker during instantiation and can be used to trigger the initialization process of the assistant;
When multiple controllers in the action chain use the same assistant, it is useful to restore the status.
PreDispatch () is triggered before the dispatch action.
Triggered when the postDispatch () distribution process ends-even if the preDispatch () plug-in has skipped this action. This vulnerability is frequently used during cleaning.
GetRequest () gets the current request object.
GetResponse () gets the current response object.
GetName () gets the assistant name. The class name section after the underline is obtained. if there is no underline, the full name of the class is obtained.

For example, if the class name is zend_controller_action_helper_redire, it returns Redirector. if the class name is FooMessage, it returns the full name.

Examples

Purpose: parse the entered URL and return each part. Use parse_url to parse the specified URL.
Use zendstudio to create a zend framework project helper_demo1.

New File:/helper_demo1/library/Application/Controller/Action/Helpers/UrlParser. php

<?phprequire_once 'Zend/Controller/Action/Helper/Abstract.php';class Application_Controller_Action_Helpers_UrlParser extends Zend_Controller_Action_Helper_Abstract{  public function __construct()  {  }  /**   * Parse url   *   * @param String $url   * @return Array part of url   */  public function parse($url)  {    return parse_url($url);  }}

Modify the File:/helper_demo1/application/Bootstrap. php

<? Phpclass Bootstrap extends {protected function _ initAutoload () {$ autoloader = Zend_Loader_Autoloader: getInstance (); $ autoloader-> registerNamespace (array ('application _'));} protected function _ initActionHelpers () {// use the prefix format // Zend_Controller_Action_HelperBroker: addPrefix ('application _ Controller_Action_Helpers '); // specify the directory and prefix // container :: addPath ('/www/helper_demo1/library/Application/Controller/Action/helpers', // 'application _ Controller_Action_Helpers'); // A new Helper class is used to input Zend_Controller_Action_HelperBroker :: addHelper (new Application_Controller_Action_Helpers_UrlParser );}}

Modify Test action:/helper_demo1/application/controllers/IndexController. php

<?phpclass IndexController extends Zend_Controller_Action{  public function init()  {    /* Initialize action controller here */  }  public function indexAction()  {    $urlParser = $this->_helper->getHelper('UrlParser');  var_dump($urlParser->parse('http://www.bkjia.com/article/80479.htm'));  }}

The above describes the custom action assistant class and simple usage.

Note what is the prefix of the helper class, the name of the helper class, and the assistant path.

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.