We often talk about behavior extensions and plug-ins (recommended) in ThinkPHP.

Source: Internet
Author: User

We often talk about behavior extensions and plug-ins (recommended) in ThinkPHP.

Principle Analysis

Use the import or add method in the Hook class to set the correspondence between tags and classes (for example, 'app _ init '=> array ('common \ Behavior \ InitHook, load it to the static variable $ tags in the Hook class. When the listen or exec method of the static method in the Hook is executed (exec is called in the listen method), the class corresponding to the tag is instantiated, and the corresponding method is called (if it is a plug-in, call the passed method. If it is a behavior, call the run method ).

The exec method in the Hook is defined as follows:

Static public function exec ($ name, $ tag, & $ params = NULL) {if (false === strpos ($ name ,'\\')) {// plug-in (multiple portals) $ class = "Addons \\{$ name }\{$ name} Addon ";} else {// behavior extension (only one run Entry Method) $ class = $ name. 'behavior '; $ tag = 'run';} $ addon = new $ class (); return $ addon-> $ tag ($ params );}

Behaviors in ThinkPHP

Load the correspondence between labels and Classes

// Load mode behavior definition if (isset ($ mode ['tags']) {Hook: import (is_array ($ mode ['tags'])? $ Mode ['tags']: include $ mode ['tags']);} // load the application behavior definition if (is_file (CONF_PATH. 'tags. php ') // allows the application to add the development mode configuration definition Hook: import (include CONF_PATH. 'tags. php ');

Pattern behavior correspondence

Defined in the tags tag in ThinkPHP/Mode/common. php

'Tags' => array ('app _ begin' => array ('behavior \ ReadHtmlCache ', // read static cache ), 'app _ end' => array ('behavior \ ShowPageTrace ', // page Trace display), 'view _ parse' => array ('behavior \ ParseTemplate ', // template parsing supports PHP, built-in template engine, and third-party template engine), 'template _ filter' => array ('behavior \ ContentReplace ', // template output replacement ), 'view _ filter' => array ('behavior \ writehtmlcache', // write to static cache ),)

Application behavior correspondence

Defined in Application/Common/Conf/tags. php

Call the corresponding Behavior

For example, Hook: listen ('app _ begin') is equivalent to calling the run method in Behavior \ ReadHtmlCache. Find the class and view the corresponding run method as follows:

Custom Behavior

1. Add a ing in Application/Common/Conf/tags. php.

'dqs_behavior'=>array('Common\Behavior\Dqs')

Add the ing to tags. php. The program automatically loads it to the $ tags variable of the Hook. Of course, you can also manually load it by using the add method in the Hook.

2. Define the Common \ Behavior \ DqsBehavior class

<? Phpnamespace Common \ Behavior; use Think \ Behavior; defined ('Think _ path') or exit (); class DqsBehavior extends Behavior {public function run (& $ content) {echo '<pre>'; print_r ('behavior Dqs called '); echo' </pre> ';}}

The Behavior class is the abstract class that comes with ThinkPHP.

3. Call Behavior

Results:

Custom plug-ins

1. Define the plug-in file

You can view the extension class instantiation method in the Hook file ($ class = "Addons \ {$ name} Addon, to define a plug-in named Dqs, you should define DqsAddon under the Addons/Dqs directory. class. PHP file. The procedure is as follows:

<? Phpnamespace Addons \ Dqs; class DqsAddon {public $ info = array ('name' => 'editor', 'title' => 'dqs test plug-in ', 'description' => 'mainly used for output', 'status' => 1, 'author' => 'lidequany', 'version' => '0. 1 '); public function dqsTrace ($ pa) {echo' <pre> '; print_r ($ pa); echo' </pre> ';}}

2. register the plug-in

The so-called registration plug-in is to add the ing between plug-in labels and classes to the static variable $ tags in the Hook class. For the corresponding behavior, the identifier is just an identifier and can define any identifier, but for the corresponding plug-in, the identifier cannot be defined at will, because the plug-in identifier represents the method name of the plug-in. The above plug-in registration code is as follows:

\Think\Hook::add('dqsTrace',array('Dqs'));

3. Call the plug-in

$params=array('name'=>'dqs');\Think\Hook::listen('dqsTrace',$params);

The effect is as follows:

As mentioned above, the behavior extension and plug-in (recommended) in ThinkPHP are all the content shared by Alibaba Cloud xiaobian. I hope to give you a reference and support for the customer's house.

Related Article

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.