Two configurations of thinkphp hooks and two methods of invocation

Source: Internet
Author: User
Tags tag name

thinkphp hook Behavior class is a more difficult to understand the problem, there are many online writing thinkphp hooks articles, I am also based on the online article to set the thinkphp hook behavior, But according to these online articles, I in the process of setting up, tried more than 10 times did not succeed, but , I still did not give up, finally in the side of the adjustment of details, while the test process to achieve the hook behavior settings. Here is my personal setup experience, here to share with YOU.

The individual has made two settings, both of which have been tested successfully, a simple point, in the core file of thinkphp to imitate the core behavior class added another behavior class, the following is:

My Virtual Host configuration folder is D:/think

Thinkphp is the TP framework folder, configured by the TP framework should be aware that the library is TP put core files folder, behavior folder is TP hook behavior of the core folder file, There are many behavior class files, I'm Just imitating these hooks. the class file built a hook behavior in the file adBehavior.class.php, the Following:

<? phpnamespace Behavior; class adbehavior{    function Run ($arg) {// in this introduction, run must, Careful will find in the think core Behavior.class.php inside there is such  a word        echo ' I is a '.  $arg[' name ']. ' advertisement, '. $arg [' value ']. ' Endorsement ';    }}

After setting the custom hook behavior class, We are going to use this hook behavior class to promote the behavior of the hook, "the behavior of the hook" is actually the trigger hook behavior class of the Run method inside the code execution, i here just simple output Run method parameters of the content, in the application, Everyone is free to play.

Here are the actions for the behavior of the hair trigger:

Explain: I am here is the login method of the Indexcontroller controller under the home module

namespace home\controller; usethink\controller; usethink\hook;//The introduction of the trigger hook behavior of the class, is for the following Hook::add () call to do foreshadowingclassIndexcontrollerextendsController { public functionLogin () {//Here I set a label for the ad behavior, that is, to give me the custom Adbehavior hook behavior class to add an identity for the trigger behaviorHook::add (' ad ', ' Behavior\\adbehavior '); //the first is the name of the execution label, and the second argument is the address of the class that behavesHook::add (' Test ', "home\\behaviors\\testbehavior"); //hook::add (' test2 ', "home\\behaviors\\testbehavior");        $param=Array(' name ' = ' Testbehavior '); $param 2=Array(' LIS ' = ' llistion '); $param 3=Array(' Music ' = ' Cangjingshikong ')); $this->assign (' param ',$param); $this->assign (' param ',$param 2); $this->assign (' param ',$param 3); $this-Display (); }

Here is the login template content:

<!DOCTYPE HTML Public "-//w3c//dtd XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" ><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head>    <title>thinkphp Behavior (Behavior) extensions and Plug-ins (Plug or Hooks) (with examples)</title>    <Metahttp-equiv= "content-type"content= "text/html; charset=utf-8" />    <Metaname= "description"content= "hook Use method" /></Head><Body><H1>How to used?</H1>{: Hook (' ad ', array (' name ' = ' AV ', ' value ' = ' * Teacher ')}<Div>———————————————— Split Line 1 ——————————————————</Div><Divstyle= "font-weight:bold; color: #00ff00">{: Hook (' ad ', array (' name ' = = ' MV ', ' value ' = ' Cang aoi teacher ')}</Div><Div>———————————————— Split Line 2 ——————————————————</Div>    <!--hook function The first parameter is the name of the label, and the second argument is arbitrary -    <!--the first parameter of the tag function is the name of the label, and the second must be a variable because the tag function is the argument passed by the Address. -<Div>Hook function: {: Hook (' Test ', array (' name ' = ' World ')}<HR/></Div><Div>Tag function: {: tag (' mv ', $param)}<HR/></Div><HR/><Div>Tag function: {: tag (' action_begin ', $param)}</Div></Body></HTML>

Here's the path I will not write it, the general understanding of the TP Framework People know, This template login.html put Where.

About the content of this template, I have the following to tell the next one of the settings of the hook behavior method of the content is also put here, you can test yourself to call this template, if you follow my idea set up, I think it should be output:

  

How to used?

I am an AV advertising, * teacher Endorsement

———————————————— Split Line 1 —————————————————— I am a mv advertising, Cang Aoi Teacher Endorsement ———————————————— Division line 2 —————————————————— oh, here to quote the role of teacher, you understand. Needless to say, the following is the second way to set their own behavior, here I am based on the TP Framework Handbook and the net text of your netizens to test successfully, you follow my thinking set on the Line.  Only you configured the right, I think there should not be too many problems.  1, first in your module to build a folder behaviors, and then in the behaviors inside create a custom hook behavior class, The hook behavior class must be xxxBehaviors.class.php format.  2. next, Add a file tags.php in the Conf folder below your Module.  Why do this, This is the TP syntax, you do the right, of course, you can also explore.  This said may not be enough image, below I still on the code bar, I describe the comparison is Poor. First cut a map to show the path, lest some students still can not touch the Head.

  Explain the above folder, conf folder some TP will be automatically generated, some may want you to create new, Behaviors folder is to you new, the following code: inside the Behaviors folder (full path D:\think\application\Home \behaviors\testbehavior.class.php) the contents of my custom hook behavior class:
 <? php  /*  * * Created by Phpstorm. * user:administrator * DATE:2016/7/17 * time:11:02  */ namespace home\behaviors;  /*   * Register the hook behavior class, the behavior to trigger is written in the Run function *  */ class  testbehavior extends   \think\behavior{ public   function  run (& $arg   echo  ' test behavior===== the next line is parameter <br/> '.  $arg     [' name ' 

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

  

<? PHP /*  */returnarray(
// ' action_begin ' =>array (' home\\behaviors\\test ', ' home\\behaviors\\test '),
   //a Tag bit can have multiple behaviors, use an array.          ' Action_begin ' = =array(' home\\behaviors\\testbehavior ', ' home\\behaviors\\testbehavior '),   ' mv ' = >array(' home\\behaviors\\mvbehavior ', ' home\\behaviors\\mvbehavior '));

explain, the contents of the above tags.php file, i configured two behavior id, "action_begin", "mv", is corresponding to login.html there, you can go back to examine carefully, login.html template content, must contain " Action_begin "," MV ", Here is also the TP framework in the tags.php set the behavior of the syntax, if you want to ask why you can explore, there is not much to Say.

Also please note: the above that I commented out of the ' Action_begin ', in fact, my version is not suitable for this syntax, this is the other version in the tags.php configuration behavior identifier of the syntax, here TP inside the document said more clearly, I will not say More. If your version and I are different, then please use the above syntax to try, if you use the above syntax configuration or no way to configure success, then you may be configured Incorrectly.

configured, then try calling the template login.html again. I'm Sure you'll get the configuration results you want.

Here is a reminder, please read my comments carefully, there can be said to contain a lot of information.

next, I want to talk about the two ways to set the listener behavior class Hook (), tag ():

Both functions are built-in functions of the TP built-in listener behavior class, which can be found in the function library functions.php file of the TP Framework. Here is only the difference between their invocation, in fact, has been mentioned in the login.html template file, here again to Explain:

Two functions in the functions.php code:

/** * Get and set configuration parameters support Bulk definition * @param string|array $name configuration variable * @param mixed $value configuration value * @param mixed $default default value * @return Mixed*/functionHook$hook,$params=Array()) {\think\hook:: Listen ($hook,$params);//listen to a hook}/** * processing Tag extension * @param string $tag tag name * @param mixed $params incoming parameter * @return void*/functionTag$tag, &$params=NULL) {\think\hook:: Listen ($tag,$params);}

The reason for showing the contents of these two functions here is to explain their differences:

Careful you should find that the $param parameter value of the tag function is preceded by a ... & symbol, There is no mistake: this symbol is the address symbol of the reference variable, so the $param parameter of the tag function can only be a variable, please do not doubt, you can test the situation is not a variable, hehe.

The difference is that the hook can reference any parameter, and tag can only refer to the argument of the variable

In the above tags.php file I have repeatedly configured the Testbehavior custom class after each label behavior, is to prove that a label can also correspond to multiple behavior classes, If you configure the successful, then you run login.html, you must find the login according to Testbehavio The R setting was performed two Times.

finally, summarize:

In fact, I would like to say that the hook behavior of the TP framework is nothing more than to be in a particular place, a specific time to promote a certain behavior, this is a very wide application, I think So. For example, If you want to embed an ad in a template, you can simply set it to Me.

then, simply introduce the reference steps for the TP behavior:

First of all, you need to first configure your own behavior class, which is beyond Doubt.

Next is the act of promoting, you must set in the run of your custom behavior class, where you implement any of your behavior, the Run method is where you configure the Behavior.

then, is the configuration tags.php behavior identified, Here you can follow the above format configuration, of course, you have not carefully found the above IndexController.class.php inside I wrote a few lines of code:

The first one is the name of the execution label, the second parameter is the address of the class of the behavior  hook::add (' test ', "home\\behaviors\\testbehavior");

In fact, This is the code to dynamically add behavior identification, that is, the tag behavior of the hook behavior, that is, the logo can also be added dynamically, you can try.

finally, is the monitoring behavior, listening behavior is to refer to the tag method or hook method, where you can pass in parameters, also can not pass, see what you want to do. Listening behavior you can listen to the template, you can listen to other places, such as the method of the controller, here is not an example, you can try it yourself, I am in the template for example, completely to see the effect, after all, do not see the results of the experiment you want to prove that your experiment is a successful Experiment.

More to say, I am more careless, if there is any text error also please point out, or their own brain repair, hehe.

Two configurations of thinkphp hooks and two methods of invocation

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.