thinkphp behavior extension and plug-in mechanism introduction

Source: Internet
Author: User

First, the concept of behavioral expansion is one of the core components of the TP architecture, and I will summarize the explanation of the behavior:
TP goes through many steps during the process from accepting to HTTP requests to eventually outputting the view, which you can see in http://document.thinkphp.cn/manual_3_2.html#system_process This is no longer described in detail, then the behavior extension is actually buried in these processes a hook, you can add your own business logic to the hook, when the program executes to a hook location will automatically trigger your business logic, about the system preset hooks can refer to:
Http://document.thinkphp.cn/manual_3_2.html#behavior_extend

The main purpose of this article is to teach you how to use the behavior extension and plug-ins, why should we mention the word "plugin" here? Because in 3.2, with a plug-in concept, and behavior shared a hook class, so put together to say, the principle is not much different.

First, the Behavior extension chapter
1.TP built-in behavior hooks
We know that some behavior hooks are reserved in the TP, such as Action_begin, which is triggered when the action is started, the hooks are pre-set by the configuration file registration behavior class, everyone inapplication/common/confDirectory to create a tags.php, this and 3.1 is the same, return an array, array format is "Hook name" =>array ("behavior Class 1", "Behavior Class 2" ...)
Here I give an example:
application/common/conf/tag.php:
<?php
Return Array (
"Action_begin" = = Array ("Behaviors\\test")
);
?>
Can see, I go to action_begin this hook inside register a behavior, this behavior is behaviors\\test here is the name space notation, its corresponding class file path is:
application/behaviors/testbehavior.class.php
Note that the actual class file name needs to be appended with the behavior suffix and with. class.php as the file name extension.
application/behaviors/testbehavior.class.php:
<?php
namespace behaviors;
Class testbehavior{
function Run ($arg) {
echo "This is a behavior extension". $arg;
}
}
?>
Be aware of the namespace of the first row, and consult the PHP manual for the namespaces you don't understand. In addition, for the automatic loading mechanism of TP, refer to the manual:
Http://document.thinkphp.cn/manual_3_2.html#autoload
The execution entry for the behavior is the run () method, which automatically executes the run () method in the behavior class when the hook is triggered.

2. Adding hooks and registering behavior dynamically
Through the above example, we understand the behavior is probably something, but for its implementation process may not be clear, here I introduce the dynamic add hooks and registration behavior, so that we have a relatively clear understanding of the behavior execution mechanism.
First, the behavior hooks add and register the behavior class, as well as the triggering behavior, all is through the hook class to realize, the hook class in the TP core package in the thinkphp directory, Hook.class.php.
>> Add hooks and register behavior: \think\hook::add (' Hook name ', ' behavior ')
>> Buried/monitor/trigger Hooks: \think\hook::listen (' Hook name ', ' parameter passed to run, must be a variable ');
Suppose we need to access the index.php/public/ Login.html trigger the behavior in the login hook, then first we need to listen to the hook in the login method, that is, the hook is buried in the login method, when accessing the login method will automatically trigger, a bit like a hunter's trap is not it?
function Login () {
\think\hook::listen (' login ');//listen for a hook called login
... Other code slightly ...
}
OK, we listen to the login hook in the login method, then we add some behavior to this hook, so that when accessing login, it will automatically trigger these behavior, execute the behavior class of the Run method.
Where does the behavior register?
Of course, you have to register before the trigger, 1. You can register through tags.php, mentioned above, just change action_begin to login. 2. Dynamically add, if you use this hook only in the public controller, then you can dynamically add hooks in Publiccontroller's _initialize () initialization method.
function _initialize () {
\think\hook::add (' login ', ' behaviors\\test ');
}
Here to login this hook added a test behavior, more lazy directly from the above copied over, we understand this meaning can be.
If you add multiple behaviors, you can do this.
\think\hook::add (' login ', array (' behaviors\\test ', ' behaviors\\test1 ' ...));
is the second parameter becomes an array, each element of the array corresponds to a behavior class, and note that when the hook is triggered, all of the behavior is executed sequentially.

3. Behavior with parameters
Above we know that the behavior is carried out through the run () method, then we want to pass some parameters in what to do?
The answer is \think\hook::listen (); The second parameter.
Note The Listen method is defined as follows:
static public Function Listen ($tag, & $params =null)
You can see that the second parameter is a reference passed parameter, that is, the second argument must be a variable, cannot be a value, the following method is wrong:
\think\hook::listen (' login ', ' hello '); X
This is the right thing.
$hello = "Hello";
\think\hook::listen (' login ', $hello);//√
Knowledge about reference passing is not covered here, please read the PHP manual yourself.

So we're in the behavior class. The Run method can specify a parameter to receive the $hello
function Run ($arg) {
echo $arg;//Output Hello
}
And of course you can.
Function run (& $arg) {
echo $arg;//Output Hello
$arg = "Bye";
}
This parameter is set to the reference type, and you can change the value of the original variable inside the run.

Second, plug-in
Looking at the above behavior, we can summarize a pattern:
Define different behaviors, execute the same method run
And what about plugins?
It can be defined as run, and it is equivalent to having multiple portals within a behavior class that are triggered under different conditions.
Suppose we add such a hook inside the tags.php.
' Showflash ' = = Array (' Test '),
Notice the difference between the behavior, the registration behavior needs to include the namespace is the backslash \ \, when there is no backslash, only one word will be considered a plug-in.
Plugin definition in application/addons/plug-in name/plugin name Addon.class.php
For example, the test plugin above is application/addons/test/testaddon.class.php
Definition of plug-in class:
<?php
namespace Addons\test;
Class testaddon{
function Showflash () {
echo "This is the plug-in execution portal";
}
}
?>
One obvious difference is that the run entry becomes Showflash, which is the same as the hook name.
Is there any doubt about that? Please keep looking down:

Above we only defined a plug-in hook Showflash, if we come up with a hook:

' Clearflash ' =>array (' test ');

Did you see that? This hook also registers a test plug-in, and this test is the Testaddon class above, the difference is that you need to define an entry method for this clearflash, so the Testaddon class becomes:
<?php
namespace Addons\test;
Class testaddon{
function Showflash () {
echo "This is the plug-in execution portal";
}

function Clearflash () {
echo "This is another plug-in hook entry";
}
}
?>
So when you have different hooks registering the same plug-in class, you need to define the entry method for the hooks in the plug-in class separately.
The behavior class is not, the behavior class regardless of whether you are the same hook, it only look for the Run method.
This is the difference between behavior and plug-ins.

Note:

1, need to add TP configuration items

' Autoload_namespace '       = =  Array (' Addons ' = './addons/'),//List of extension modules

thinkphp behavior extension and plug-in mechanism introduction

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.