Two configuration calling methods for hooks in thinkPHP are described in detail.

Source: Internet
Author: User

Two configuration calling methods for hooks in thinkPHP are described in detail.

This article describes two configuration call methods for hooks in thinkPHP. We will share this with you for your reference. The details are as follows:

Thinkphp's hook behavior class is a problem that is hard to understand. There are many thinkphp hook articles on the Internet. I also set thinkphp's hook behavior based on online articles, however, according to these online articles, I failed to try the settings more than a dozen times. However, I still did not give up and finally adjusted the details, during the experiment, the hook behavior settings are implemented. The following is my personal setup experience. I would like to share with you here.

I tried both settings successfully. in a simple way, I added another behavior class to the thinkphp core file to imitate the core behavior class. below is:

My VM configuration folder is D:/think

Thinkphp is the tp framework folder. Once configured, the tp framework should know that the Library is the folder where the tp core files are stored, and the Behavior folder is the core folder of the tp hook Behavior file, there are a lot of behavior files in it. I just copied these hook behavior files and created a hook behavior file adBehavior. class. php:

<? Phpnamespace Behavior; class adBehavior {function run ($ arg) {// This section describes what is required for run. You will find Behavior at the Think core carefully. class. in php, there is such a sentence abstract public function run (& $ params); echo 'I am my '. $ arg ['name']. 'ad ,'. $ arg ['value']. 'endorsement ';}}

After setting the custom hook behavior class, we will use this hook behavior class to promote hook sending, '''hook behavior ''' to put it bluntly, it is actually the code execution in the run method of the promotion hook behavior class. Here I am simply outputting the content of the run method parameter, in the application, everyone can play it freely.

The following are the actions to promote the release:

Here is the logon method of the IndexController controller in the Home module.

Namespace Home \ Controller; use Think \ Hook; // introduces the class that promotes the Hook action, which is for the following Hook: add () call the class IndexController extends Controller {public function login () {// here I set a label for the Active Directory action, that is to say, add an issued identity Hook: add ('ad', 'behavior \ adBehavior ') to the custom adBehavior Hook Behavior class '); // The first parameter is the name of the execution label, and the second parameter is the address Hook of the behavior class: add ('test', "Home \ Behaviors \ testBehavior "); // Hook: add ('test2', "Home \ Behaviors \ testBehavior"); $ param = array ('name' => 'testbehavior '); $ param2 = array ('Lis '=> 'llistion'); $ param3 = array ('music' => 'cangjingshikong'); $ this-> assign ('param ', $ param); $ this-> assign ('param', $ param2); $ this-> assign ('param', $ param3 ); $ this-> display ();}}

The following is the content of the login template:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

I will not write this article here. I generally know all the people who have understood the tpframe. Where is this template login.html.

For the content of this template, I have already put the content of another method for setting hook behavior in advance. You can test and call this template by yourself, if you set it with my idea, I think it will output:

How to used? I am an AV advertisement, * instructor endorsement ---------------- split line 1 ------------------ I am a music video advertisement, and the instructor's endorsement ---------------- split line 2 ------------------

Well, here is a reference to the role of Mr. Cang, you know.

Not to mention nonsense. The second method of self-setting behavior is as follows. Here I have successfully tested it based on the tp framework manual and the net texts of some netizens, you just need to set it according to my ideas. Only you configured it correctly. I don't think there will be too many problems.

1. Create a folder named Behaviors under your module, and create a custom hook behavior class in Behaviors. The hook behavior class must be in the format of xxxBehaviors. class. php.

2. Add a new file tags. php In the Conf folder under your module.
Why? This is the tp syntax. You just need to do it. You can also explore it yourself.

In this case, the image may not be good enough. I will go to the code below, and my description is poor.

Cut a graph to show the path, so that some students may be confused.

To explain the above folder, some tp in the Conf folder will be automatically generated, some may need to be created, and the Behaviors folder will need to be created. The Code is as follows:

In the Behaviors folder (full path D: \ think \ application \ Home \ Behaviors \ testBehavior. class. php), the content of the custom hook behavior class is as follows:

<? Php/*** Created by PhpStorm. * User: Administrator * Date: 2016/7/17 * Time: */namespace Home \ Behaviors;/** register the hook behavior class, the action to be triggered is written in the run function **/class testBehavior extends \ Think \ Behavior {public function run (& $ arg) {echo 'test behavior ==== the next line is the parameter <br/> '. $ arg ['name'] ;}}

In the Conf folder (full path D: \ think \ application \ Home \ Conf \ tags. php, of course this is my situation) content of tags. php:

<? Php/*** Created by zhuowenfeng. * User: Administrator * Date: 2016/7/17 * Time: */return array (// 'Action _ begin' => array ('home \ Behaviors \ test ', 'Home \ Behaviors \ test'), // a tag can have multiple Behaviors. Use an array. // For version 3.2.1, you need to change it to 'Action _ in '=> array ('home \ Behaviors \ testBehavior', 'home \ Behaviors \ testBehavior '), 'mv '=> array ('home \ Behaviors \ mvBehavior', 'home \ Behaviors \ mvBehavior '));

The contents of the authorization template must contain ''action _ in ''and ''mv'', which is also the tp framework in tags. php sets the Behavior Identification syntax. If you want to ask why you can explore it on your own, I will not talk about it here.

Note: The 'action _ in 'That I commented out above is actually not suitable for this syntax in my version. This is another version in tags. the syntax for configuring the Behavior Identification in php. The documents in tp here are clear and I will not talk about it much. If your version is different from me, try using the above syntax. If the above syntax configuration still cannot be configured successfully, it may be that you configured it incorrectly.

After configuration, try using the login.html template. I believe that you will be able to get the configuration result you want.

Here, I would like to remind you to carefully read the comments I have made, which can also contain a lot of information.

Next, let's talk about hook () and tag () methods for setting the listener hook behavior class ():

Both functions are built-in functions of the tp built-in listener hook behavior class, which can be found in the functions. php file of the tp framework function library. Here, we only talk about the differences between their calls, which have already been mentioned in the login.html template file. Here we will explain it again:

Code of the two functions in functions. php:

/*** You can obtain and set configuration parameters in batches. * @ param string | array $ name configuration variable * @ param mixed $ value configuration value * @ param mixed $ default value * @ return mixed */function hook ($ hook, $ params = array () {\ Think \ Hook: listen ($ hook, $ params ); // listen to a hook}/*** process tag extension * @ param string $ tag name * @ param mixed $ params input parameter * @ return void */function tag ($ tag, & $ params = NULL) {\ Think \ Hook: listen ($ tag, $ params );}

The reason why the two functions are shown here is to explain their differences:

Be careful, you should find that the $ param parameter value of the tag function is preceded by a symbol, which is the address symbol of the referenced variable, therefore, the $ param parameter of the tag function can only be a variable. Do not doubt it. You can test whether it is a variable.

The difference between them is that the hook can reference any parameter, and the tag can only reference the parameter of the variable.

When logging, you must find that login is executed twice according to the testBehavior settings.

Finally, let's summarize:

In fact, I would like to say that the hook behavior class of the tp framework is nothing more than to promote certain behavior at a specific location or at a specific time. This is widely used, so I think so. For example, if you want to implant an advertisement in a template, you can simply set it to me.

Next, we will briefly introduce the reference steps of tp behavior:

The first step is to configure your behavior class first, which is beyond doubt.

The second is the promotion behavior. You must set it in the run of your custom behavior class to implement any behavior here. The run method is the place where you configure the behavior.

Then, configure tags. php behavior identifier. Here you can configure according to the above format. Of course, are you sure you have noticed the above IndexController carefully. class. I wrote several lines of code in php:

// The first parameter is the name of the execution label, and the second parameter is the address Hook of the behavior class: add ('test', "Home \ Behaviors \ testBehavior ");

In fact, this is the code for dynamically adding behavior identifiers, that is, the tag behavior of hook behavior, that is, the identity can also be dynamically added. You can try it.

Finally, it is the listening behavior. The listening behavior is to reference the tag method or the hook method. Here you can pass in the parameter or not to see what you want to do. You can listen on the template or in other places, for example, in the Controller method. I will not give an example here. You can try it on your own, it is entirely for the purpose of seeing the results. After all, you cannot see the results you want in the experiment, but it cannot prove that your experiment is a successful experiment.

I am very careless. If you have any text errors, please point them out or make up your mind.

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.