Zend framework creates its own action assistant in detail, zendframework
This example describes how the Zend framework creates its own action helper implementation. Share to everyone for your reference, as follows:
the abstract base class for assistants is zend_controller_action_helper_abstract, which requires inheriting this class if you want to define your own helper .
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_action $_actioncontroller * * protected $_actioncontroller = null; /** * @var Mixed $_frontcontroller */protected $_frontcontroller = NULL; /** * Setactioncontroller () * * * @param zend_controller_action $actionController * @return Zend_controller_actionhe Lper_abstract provides a fluent interface */Public function Setactioncontroller (zend_controller_action $actionControll ER = null) {$this->_actioncontroller = $actionController; return $this; }/** * Retrieve current Action Controller * * @return zend_controller_action */Public function Getactioncontro Ller () {return $this->_actioncontroller; }/** * Retrieve Front Controller Instance * * @return zend_controller_front * * Public Function Getfrontcontroll ER () {return Zend_controlLer_front::getinstance (); }/** * Hook into action Controller initialization * * @return void */Public function init () {}/** * Hoo K into Action Controller predispatch () workflow * * @return void */Public Function Predispatch () {}/** * Ho OK into action Controller postdispatch () workflow * * @return void */Public Function Postdispatch () {}/** * Getrequest ()-* * @return zend_controller_request_abstract $request */Public Function getrequest () {$contro Ller = $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 helper base class provides the following common methods:
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 the helper;
When multiple controllers in the action chain use the same assistant, it is useful to restore the state.
Predispatch () is triggered before the action is distributed.
Postdispatch () triggers at the end of the distribution process-even if the Predispatch () plugin has skipped the action. Used extensively when cleaning.
Getrequest () Gets the current request object.
GetResponse () Gets the current response object.
GetName () Gets the helper name. Gets the class name part after the underscore, without underlining gets the full name of the class.
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.
Examples of custom Action helper classes
Function: Parses the incoming URL and returns the various parts. Use Parse_url to parse the specified URL.
Create a new Zend Framework project Helper_demo1 with Zendstudio.
Added Files:/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);} }
Modified files:/helper_demo1/application/bootstrap.php
<?phpclass Bootstrap extends zend_application_bootstrap_bootstrap{ protected function _initautoload () { $autoloader = Zend_loader_autoloader::getinstance (); $autoloader->registernamespace (Array (' Application_ ')); } protected function _initactionhelpers () { //prefixed with //zend_controller_action_helperbroker::addprefix (' Application_controller_action_helpers '); Specify directory and prefix //zend_controller_action_helperbroker::addpath ('/www/helper_demo1/library/application/controller /action/helpers ', // ' application_controller_action_helpers '); New helper class passed in zend_controller_action_helperbroker::addhelper (new Application_controller_action_helpers_ Urlparser);} }
modifying 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 helper class, as well as the simple way to use it.
It is important to note what is the prefix of the helper class, the name of the helper class, and the path of the helper.
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 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
- Zend Framework Action Assistant Redirector usage examples
- Zend Framework Action Helper URL usage detailed
- Zend Framework Action Helper JSON usage example analysis
- Zend Framework Action Assistant Flashmessenger usage
- Zend Framework Action Helper (Zend_controller_action_helper) usage
- Zend Framework implementation of Zend_view Integrated Smarty template system
- Zend Framework Tutorial's routing function zend_controller_router detailed
http://www.bkjia.com/PHPjc/1106901.html www.bkjia.com true http://www.bkjia.com/PHPjc/1106901.html techarticle The Zend framework creates its own action assistant, Zendframework This example describes how the Zend framework creates its own action helper implementation. Share to everyone for your reference, specific as ...