On thinkPHP3.2.2 framework behavior extension and the analysis of demo

Source: Internet
Author: User
This article mainly introduced the thinkPHP3.2.2 framework behavior extension, combined with the example form analysis the thinkPHP3.2.2 frame behavior expansion principle, the realization method and the related operation attention matters, the need friend can refer to the next

The examples in this paper describe the thinkPHP3.2.2 framework behavior extension. Share to everyone for your reference, as follows:

First introduce the behavior extension class, I am dull, or borrow the TP manual to say:

Behavior (Behavior) is a relatively abstract concept, you can imagine in the application execution process of an action or processing, in the framework of the execution process, each location can have behavior, such as route detection is a behavior, static caching is a behavior, user rights detection is also behavior, large to the business logic, Small to browser detection, multi-language detection and so can be regarded as a behavior, even say you want to give your site users the first access to pop hello,world! These can be seen as a behavior where the presence of the behavior allows you to change or add functionality to the perimeter by extending or configuring it without altering the framework and application.

And the different behavior also has the position common, for example, some behavior's function position is before the application executes, some behavior all is after the template output, we call these behavior function the place to be labeled as the label (bit), when the application runs to this label, will be intercepted, unifies executes the related behavior, Similar to the concept of "tangent" in AOP programming, binding related behavior to a certain facet is a kind of idea of AOP programming.

Let's get down to the bottom and explain the example of behavior (Behavior)

There are two ways to make behavioral work:

1. Through the tags.php file configuration behavior in the Conf directory through the \Think\Hook::listen(name); triggering behavior

2. By \Think\Hook::add(name,class_namespace) registering a behavior and then triggering (the registration function must precede the trigger function)

Before we explain the examples, it is necessary to first tell us how the behavior is triggered.

Behavior triggered by the \Think\Hook::listen(name) method, that method inside exactly what to do, we first look at the source code:

/** * Monitor Tag plug-in * @param string $tag tag name * @param mixed $params incoming parameter * @return void *//** * Add by Yangligao 2014/8/25 * Li Sten personally think the method name get not good understanding, see the program know this method is actually to see the $tags in the parameters of the tag *   if there is, it triggers; *   if not, you know, too (at least the program is not done). */static Public Function Listen ($tag, & $params =null) {if (Isset (self:: $tags [$tag])) {  if (app_debug) {   G ($ tag. ' Start ');   Trace (' ['. $tag. ']--start--', ' ', ' INFO ');  }  foreach (self:: $tags [$tag] as $name) {   app_debug && G ($name. ') _start ');   $result = Self::exec ($name, $tag, $params);   if (app_debug) {    G ($name. ') _end ');    Trace (' Run '. $name. ' [RunTime: '. G ($name. ' _start ', $name. ' _end ', 6). ' S] ', ' ', ' INFO ');   if (false = = = $result) {    //If return false then break plug-in execution    return;}  }  if (app_debug) {//record the behavior of the execution log   trace (' ['. $tag. ']--end--[RunTime: '. G ($tag. ' Start ', $tag. ' End ', 6). ' S] ', ' ', ' INFO ');  }//return; thinkphp original style return $result;//The author ^_^ himself added}

This method records the behavior of some running state value, the last point of the function of the author made a little change, just let this method has a return value (rather than return null)

The most important place is

$result = Self::exec ($name, $tag, $params);

This place began to carry out the so-called behavior, first look at exec the source of the method

/**  * Execute a plugin  * @param string $name plug-in name  * @param string $tag method Name (sign)  * @param Mixed $params passed in parameter  * @ return void *  //** * Add by Yangligao 2014/8/25  * Exec Execute file *  /static public function exec ($name, $tag, &am p; $params =null) {  if (' Behavior ' = = substr ($name, -8)) {   //The behavior extension must use the Run entry method   $tag = ' run ';  }  file_put_contents (' D:/1.txt ', $name, file_append);  $addon = new $name ();  Return $addon $tag ($params); }

What this method does is to determine if there is a behavior string in the class name set in the behavior configuration, and if so, execute a method called run.

It's almost ready. Try the example below

method One: configure behavior by triggering behavior through the tags.php file in the Conf directory \Think\Hook::listen(name);

Let's start with the home/conf . tags.php configuration file

<?phpreturn Array (  ' app_app ' = = Array (' Home\behavior\demoshowhelloworldbehavior '),);

According to this configuration file, we also have to prepare DemoShowHelloWorldBehavior the class file under Home/behavior

<?phpnamespace Home\behavior;use think\controller;/** * The class of this file can inherit the Controller class so that you can use the Assign method in the framework of this method: Ignore your return value , just used as a way to determine if the plugin has errors * for a certain purpose, the author made a small change to the Hook::listen method so that he can return the value, perhaps a bit with */class Demoshowhelloworldbehavior extends controller{ Public Function Run () {  $return _string = ' (Home\behavior) <font>DemoShowHelloWoldBehavior</font> is Running. ^_^!

For this class of files, we are the inherited controller, which has done two things:

1. Returns a string

2. Assigning values to a template using the Assign method of the Controller method

Then we'll trigger this behavior, you just have to write the code on the controller and output the corresponding variable in the corresponding template.

$behaviorReturn = \think\hook::listen (' App_app ');

<p>{$behavior _assign}</p>

So the page will output the value of the assign in the behavior class, the return value of the behavior class is not printed here, you are interested in printing to see OH

method Two:. By \Think\Hook::add(name,class_namespace) registering a behavior and then triggering (the registration function must precede the trigger function)

The only difference between this and the method is that he does not have to define the tags.php file, instead of a PHP statement (below):

\think\hook::add (' App_app ', ' home\\behavior\\demoshowhelloworldbehavior ');

The other operations are the same.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.