Zend framework to create your own action assistant detailed _php instance

Source: Internet
Author: User
Tags autoloader zend zend framework

The example of this article describes the Zend Framework to create its own action Assistant implementation method. Share to everyone for your reference, specific as follows:

The abstract base class for the assistant is Zend_controller_action_helper_abstract, and you need to inherit this class to define your own assistant .

The source code for 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_actio
  N $_actioncontroller */protected $_actioncontroller = NULL;
  /** * @var Mixed $_frontcontroller * * protected $_frontcontroller = NULL; /** * Setactioncontroller () * * * @param zend_controller_action $actionController * @return zend_controller_acti Onhelper_abstract provides a fluent interface */Public function Setactioncontroller (zend_controller_action $actionCo
    Ntroller = null) {$this->_actioncontroller = $actionController;
  return $this; }/** * Retrieve current Action Controller * * @return zend_controller_action/Public Function getaction
  Controller () {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 Postdis Patch () {}/** * Getrequest ()-* * @return zend_controller_request_abstract $request/Public funct
    Ion Getrequest () {$controller = $this->getactioncontroller ();
    if (null = = $controller) {$controller = $this->getfrontcontroller ();
  return $controller->getrequest (); /** * GetResponse ()-* * * @return zend_controller_response_abstract $response/Public Function Getresp
    Onse () {$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 ($fullClassName, ' \ \ ')!== false) {$helperName = STRRCHR ($fullClassName, ' \ \ ');
    Return LTrim ($helperName, ' \ n ');
    else {return $fullClassName;

 }
  }
}

The common methods provided by the assistant base class are as follows:

Setactioncontroller () is used to set the current action controller.
Init (), which is triggered by an assistant broker when instantiated, and can be used to trigger the initialization of an assistant;
When multiple controllers in the action chain use the same assistant, it is useful to recover the status.
Predispatch () is triggered before the distribution action.
Trigger at the end of the Postdispatch () distribution process-even if the Predispatch () plug-in has skipped the action. Extensive use when cleaning.
Getrequest () Gets the current request object.
GetResponse () Gets the current response object.
GetName () gets the assistant name. Gets the class name section after the underscore, and gets the full name of the class without an underscore.

For example, if the class name is Zend_controller_action_helper_redirector, he will return redirector, and if the class name is Foomessage, the full name will be returned.

Illustration of a custom action Assistant class

Function: Parse the incoming URL and return each part. Use Parse_url to resolve the specified URL.
Create a new Zend Framework project Helper_demo1 with Zendstudio.

New file:/helper_demo1/library/application/controller/action/helpers/urlparser.php

<?php
require_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 File:/helper_demo1/application/bootstrap.php

<?php
class Bootstrap extends Zend_application_bootstrap_bootstrap
{
  protected function _ Initautoload ()
  {
    $autoloader = zend_loader_autoloader::getinstance ();
    $autoloader->registernamespace (Array (' Application_ '));
  }
  protected function _initactionhelpers () {
    //prefix form
    //zend_controller_action_helperbroker::addprefix (' Application_controller_action_helpers ');
    Specifies the directory and prefix
    //zend_controller_action_helperbroker::addpath ('/www/helper_demo1/library/application/ Controller/action/helpers ',
    //                  ' application_controller_action_helpers ');
    New helper class incoming
    zend_controller_action_helperbroker::addhelper (new Application_controller_action_helpers_ Urlparser);
  }


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

<?php
class 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.jb51.net/article/80479.htm '));
  }


The above describes the custom Action helper class, and the simple way to use it.

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

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.