Two configuration invocation methods of hooks in thinkphp _php instance

Source: Internet
Author: User
Tags mixed php code php template smarty template

This article illustrates two configuration invocation methods for hooks in thinkphp. Share to everyone for your reference, specific as follows:

thinkphp hook Behavior class is a more difficult to understand the problem, the Internet has a lot of writing thinkphp Hook class articles, I was also based on the online article to set the thinkphp hook behavior, but according to these online articles, I set the process, tried more than 10 times did not succeed, but , I still did not give up, and finally in the side to adjust the details, while testing the process of implementation of the hook behavior settings. Here is my personal setup experience, here to share with you.

The individual has done two kinds of setup, all experiment succeeded, a simple point, imitate core behavior class in thinkphp core file to add another behavior class, below is screenshot:

My Virtual Host configuration folder is D:/think

Thinkphp is a TP Framework folder, CONFIGURED over the TP framework should know that the library is the folder TP put the core files, behavior folder is the hook behavior of TP file core folder, which placed a lot of behavior files, I'm just imitating these hooks. The behavior class file is built in a hook behavior class file adBehavior.class.php, which reads as follows:

<?php
namespace Behavior;
Class adbehavior{
  function Run ($arg) {
//In this introduction, run must, careful will be found in the think core Behavior.class.php have such a word abstract Public function run (& $params);
    Echo ' I am a '. $arg [' name ']. ' Advertisement, '. $arg [' value ']. ' Endorsement ';
  }
}

After you set up the custom hook behavior class, we are going to use this hook behavior to promote the behavior of the hook, ' The behavior of the hook ' is actually the act of promoting the behavior of the hook in the code execution, I'm here just a simple output run method parameters of the content, in the application, Everyone is free to play.

Here is the action to promote the behavior:

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

namespace Home\controller;
Use Think\controller;
Use think\hook;//introduces classes that promote the behavior of the hook, which is the class Indexcontroller extends for the following hook::add () invocation
Controller {public
  function login () {
    //Here I set up a label for the behavior of the ad, which is to add the identity of the Adbehavior hook behavior class to my custom
    hook::add (' Ad ', ' behavior\\ Adbehavior ');
    The first is the name of the execution label, and the second parameter is the address of the class of the Behavior
    hook::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" >  

Here's the path I do not write it, the general knowledge of the TP framework people know, this template login.html put where.

On the content of this template, I have put forward the following to be described in the next set of hook behavior method of the content also put here, you can test yourself to call this template, if you follow my thinking set, I think it should be output:

How to used?
I am an AV advertising, * teacher endorsement

———————————————— Split Line 1 ——————————————————
I am a MV advertisement, Cang Aoi teacher endorsement
———————————————— Split Line 2 ——————— ———————————

Oh, here is a reference to the role of the teacher, you understand.

Nonsense not to say, the following is the second way to set up their own behavior, here I am based on the TP framework of the manual as well as the network of users to test their own success, everyone follow my ideas set on the line. Only you configure the right, I think there should be no too many problems.

1, first in your module to build a folder behaviors, and then in the behaviors to create a custom hook behavior class, the hook behavior class must be xxxBehaviors.class.php format.

2, next in your module, the Conf folder in a new file tags.php.
Why do this, this is the syntax of TP, you do it, of course, you can also explore.

Such a son said perhaps not enough image, below I am still on the code, I describe relatively poor.

First cut the figure to show the path, so that some students still do not touch the mind.

Explain the above folder, conf folder some TP will automatically generate, and some may want you to create new, behaviors folder is to you new, the following or on the 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 hook Behavior class, the action 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
/**
 * Created by Zhuowenfeng.
 * User:administrator
 * DATE:2016/7/17
 * time:10:56/return
Array (
//' Action_begin ' =>array (' Home\\behaviors\\test ', ' home\\behaviors\\test '),
  //A label bit can have more than one behavior, using an array.
  //If it is version 3.2.1, it needs to be changed to
  ' Action_begin ' =>array (' home\\behaviors\\testbehavior ', ' home\\behaviors\\ Testbehavior '),
  ' mv ' =>array (' home\\behaviors\\mvbehavior ', ' Home\\behaviors\\mvbehavior ')
);

To explain, the contents of the above tags.php file, I configured two behavioral identification, ' Action_begin ', ' mv ', is corresponding to the login.html there, you can go back to look carefully, login.html template content, must contain " Action_begin ', ' mv ', this is also the TP framework in the tags.php set behavior identification syntax, if you want to ask why you can explore, here is not much to say.

Also please note: The above is I commented out of the ' action_begin ', in fact, my version is not suitable for this syntax, this is the other version of the tags.php configuration behavior identification 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 the success, then may be your configuration is wrong.

Configured well, then please try calling template login.html again. I'm sure you'll get the configuration results you want.

Here's a reminder, please look at the comments I've made, and there's a lot of information that can be said.

Next, I'm going to talk about two ways to set up the Listener Behavior Class hook (), tag ():

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

Two functions in functions.php code:

/**
 * Get and set configuration parameters support Batch definition
 * @param string|array $name configuration variable
 * @param mixed $value configuration value
 * @param mixed $default The default value
 * @return Mixed *
/function Hook ($hook, $params =array ()) {
  \think\hook::listen ($hook, $params); /Monitor a hook
}
/**
 * processing tag extension
 * @param string $tag label name
 * @param mixed $params incoming parameter
 * @return void< c15/>*/
function Tag ($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 the tag function of the $param parameter value in front of a ... & Symbol, there is nothing wrong: this symbol is a reference to the variable address symbol, so the tag function $param parameters can only be variables, please do not doubt, you can test the situation is not variable, hehe.

The difference is this, the hook can reference any argument, tag can only refer to the arguments of the variable

The top of the tags.php file inside me after each label behavior is repeatedly configured Testbehavior custom class, is to prove that a label can also correspond to multiple behavior classes, if you configure successfully, then you run login.html, you will definitely find login according to Testbehavio The R setting was executed two times.

Finally, summarize:

In fact, I would like to say that the TP framework hook behavior is simply to be in a specific place, a specific time to promote a certain behavior, this is widely used, 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 TP behavior:

First of all, you have to configure your behavior class first, this is beyond doubt.

The second is the act of promoting, which you have to set in the run of your custom behavior class, where you implement any of your actions, and the Run method is where you configure your behavior.

Then, is to configure tags.php behavior identification, where you can be configured according to the above format, of course, you have not carefully found above the IndexController.class.php inside I wrote a few lines of code:

The first is the name of the execution label, and the second parameter is the address of the class of the Behavior
hook::add (' Test ', ' Home\\behaviors\\testbehavior ');

In fact, here is the dynamic Add behavior identification code, that is, the hook behavior of the label behavior, that is, the logo can be dynamically added, you can try.

Finally, is the listening behavior, listening behavior is to refer to the tag method or Hook method, where you can pass in the parameters, also can not pass, see what you want to do. You can listen on the template, or you can monitor it elsewhere, for example, the Controller method, here is not an example, you can try, I am in the template for example, is entirely 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.

To say one more, I am more careless, if there is any text error also please point out, or own brain complement, hehe.

More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course", "thinkphp Template Operation Skills Summary", "thinkphp Common Methods Summary", "Smarty Template Introductory Course" and "PHP template technology Summary."

I hope this article will help you with the PHP program design based on thinkphp framework.

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.