Behavior extensions and plugins in thinkphp

Source: Internet
Author: User
Here is a cliché about the behavior extensions and plugins in thinkphp (recommended). Small very good, now share to everyone, also for everyone to make a reference.

Principle Analysis

The correspondence between the label and the class, such as ' App_init ' =>array (' Common\behavior\inithook '), is loaded into the static variable $tags in the hook class via the import or Add method in the Hook class. When executing a static method listen or Exec method in a hook (called Exec in the Listen method), instantiate the corresponding class of the tag, call the corresponding method (if it is a plug-in, call the passed method, if it is the 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, ' \ \ ')) {      //plugin (multiple entries)      $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);  

Behavior in the thinkphp

Load the correspondence between tags and classes

The load mode behavior defines if (Isset ($mode [' tags ')]) {Hook::import ($mode [' tags ']) {is_array ") $mode [' tags '  ]:include $mode [' tags ' ]);} Load app behavior definition if (is_file (conf_path. ' tags.php '))  //Allow app to add development mode configuration definition  hook::import (include Conf_path. ' tags.php ') ;

Pattern Behavior Correspondence Relationship

defined in tags tags 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 static cache    ),  )

Apply Behavior Correspondence Relationship

Defined in application/common/conf/tags.php

Invoke the corresponding behavior

such as Hook::listen (' App_begin ') is equivalent to calling the Run method in Behavior\readhtmlcache. To find the class, you can view its corresponding run method as follows

Custom behavior

1. Add Correspondence in application/common/conf/tags.php

' Dqs_behavior ' =>array (' Common\behavior\dqs ')

Add the correspondence to the tags.php, and the program will automatically load it into the $tags variable of the hook, or it can be manually loaded by using the Add method in the hook.

2. Defining 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 (' called Behavior Dqs ');    Echo ' </pre> ';  }}

Where the behavior class is an abstract class with thinkphp

3. Call behavior

Effects such as

Custom Plugins

1. Defining plug-in Files

By looking at how the plug-in class is instantiated in the hook file ($class = "addons\\{$name}\\{$name}addon";), you know that if you want to define a plug-in named Dqs, you should addons/ The DqsAddon.class.php file is defined in the DQS directory. The procedure is as follows:

<?phpnamespace addons\dqs;class dqsaddon{public    $info = Array (      ' name ' = ' Editor ',      ' title ' = ' = ') Dqs Test plugin ',      ' description ' = ' = ' mainly used for output ',      ' status ' =>1,      ' author ' = ' Lidequan ',      ' version ' = ' 0.1 '  );  Public Function Dqstrace ($PA) {    echo ' <pre> ';    Print_r ($PA);    Echo ' </pre> ';  }}

2. Registering plugins

The so-called registration plug-in, is the plug-in label and the corresponding relationship between the class, add to the hook class static variable $tags. The corresponding behavior, the identifier is just an identifier, you can define any identifier, but the corresponding plug-in, identifiers can not be arbitrarily defined, because the plug-in identifier represents the plug-in method name. So the above plugin registration code is as follows

\think\hook::add (' Dqstrace ', Array (' Dqs '));

3. Calling plug-ins

$params =array (' name ' = ' Dqs '); \think\hook::listen (' Dqstrace ', $params);

The effect is as follows

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.