This paper mainly introduces the behavior extension and plug-in principle analysis in thinkphp, the behavior in thinkphp, the custom behavior, the custom plug-in, and so on, to provide reference for everyone, hoping to help you.
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 the hook ( listen
called in the method exec
), 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 thinkphp the corresponding relationship between the tag and the class
Load mode behavior definition
if (Isset ($mode [' tags ')]) {Hook::import ($mode [' tags ']) $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 support PHP, built-in template engine, and third-party template engine ), ' Template_ Filter ' = = Array ( ' behavior\contentreplace ',//template output substitution ), ' view_filter ' = = Array ( ' Behavior\writehtmlcache ',//write to the static cache), )
Apply Behavior Correspondence Relationship
Defined in application/common/conf/tags.php
Invoke the corresponding behavior
Hook::listen('app_begin')
equivalent Behavior\ReadHtmlCache
to the Run method in the call. To find the class, you can view its corresponding run method as follows
Custom Behavior 1. Add a corresponding relationship 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
customizing plug-ins 1. Defining plug-in Files
By looking at the plug-in class instantiation method () in the hook file, $class = "Addons\\{$name}\\{$name}Addon";
you know that if you want to define a plug-in named Dqs, you should define the DqsAddon.class.php file in the Addons/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
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 the hook ( listen
called in the method exec
), 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 thinkphp the corresponding relationship between the tag and the class
Load mode behavior definition
if (Isset ($mode [' tags ')]) {Hook::import ($mode [' tags ']) $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 support PHP, built-in template engine, and third-party template engine ), ' Template_ Filter ' = = Array ( ' behavior\contentreplace ',//template output substitution ), ' view_filter ' = = Array ( ' Behavior\writehtmlcache ',//write to the static cache), )
Apply Behavior Correspondence Relationship
Defined in application/common/conf/tags.php
Invoke the corresponding behavior
Hook::listen('app_begin')
equivalent Behavior\ReadHtmlCache
to the Run method in the call. To find the class, you can view its corresponding run method as follows
Custom Behavior 1. Add a corresponding relationship 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
customizing plug-ins 1. Defining plug-in Files
By looking at the plug-in class instantiation method () in the hook file, $class = "Addons\\{$name}\\{$name}Addon";
you know that if you want to define a plug-in named Dqs, you should define the DqsAddon.class.php file in the Addons/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 recommendations:
Detailed thinkphp how to implement generate and verify verification code
How to deploy THINKPHP5 projects on cloud virtual hosts
ThinkPHP5 using the Laypage page plug-in to implement the list paging function _php instance