thinkphp Source Read 3------behavior extension

Source: Internet
Author: User

Thinkphp the core of the framework is the CBD, that is, the core core+ behavior behavior+ Drive driver, the core is the core of the entire framework model, most of them are some base class, to rule, behavior is the behavior, It is called in the specified place to complete some specific behavioral functions, and the driver driver is similar to cache caching driver, MYSQLDB database driver, etc., complete the function

The behavior is detailed in 13.1 of the manual. I'm just going to write some examples. The method that invokes the behavior.

The calling method is tags ()


/** * processing Tag extension * @param string $tag * Tag name * @param mixed $params * Incoming parameter * @return mixed */function tag ($t    AG, & $params = NULL) {//system label Extension $extends = C (' extends. $tag);    Apply label Extension $tags = C (' tags. '. $tag); if (! empty ($tags)) {if (Empty ($tags [' _overlay ']) &&! empty ($extends)) {//merge extension//a        Rray_unique the duplicate value in the divisor group $tags = Array_unique (Array_merge ($extends, $tags));        } elseif (Isset ($tags [' _overlay '))} {//By setting ' _overlay ' =>1 override system label unset ($tags [' _overlay ']);    }} elseif (! empty ($extends)) {$tags = $extends; if ($tags) {if (app_debug) {G ($tag.            ' Start '); Trace (' ['. $tag.        ']--start--', ' ', ' INFO ');                }//Execute extension foreach ($tags as $key + $name) {if (! Is_int ($key)) {//Specify the full path of the behavior class for pattern expansion            $name = $key; } B ($name, $params); } if (App_debug) {//records the behavior of the execution log trace (' ['. $tag. ']--end--[RunTime: '. G ($tag. ' Start ', $tag. ' End ', 6).        ' s] ', ' ', ' INFO ');    }} else {//did not perform any behavior that returns false to false; }}


When tag is called, you can specify override system behavior. The B function is then invoked to instantiate the behavior object.

/** * Perform a behavior * * @param string $name *          behavior name * @param Mixed $params *          incoming parameter * @return void */function B ($name, & ; $params = NULL) {    if (Strpos ($name, '/')) {        list ($name, $method) = explode ('/', $name);    } else {
   
     $method = ' run ';    }    $class = $name. ' Behavior ';    if (app_debug) {        G (' Behaviorstart ');    }    $behavior = new $class ();    $behavior-$method ($params);    if (app_debug) {//record the behavior of the execution log        G (' behaviorend ');        Trace ($name. ' Behavior:: '. $method. ' [RunTime: '.] G (' Behaviorstart ', ' Behaviorend ', 6). ' s] ', ' ', ' INFO ');}    }
   


The B function can instantiate the behavior object and pass in the parameters required by the behavior. The parameters that the behavior requires can be changed in the configuration file config.php. and overrides the default parameters in the behavior.

The base class of the behavior in lib/conf/behavior.class.php

Protected $options = Array (); Parameters for saving behavior

__construct initializes the class, and the parameter is assigned a value. The main is the assignment, if it exists in the config.php, it is covered with config.php.

__get get parameter Get behavior parameter

Run Behavior unique Execution portal

In the B function, you can also manually specify the parameter execution portal by means of B (' class name/method name ')

The default behavior is:

Checkroute Detect routing, route matching
Contentreplace Template content Output substitution
Parsetemplate Template parsing
Readhtmlcache Static cache Reads
Showpagetrace Page Trace Display
Showruntime Run time display
Tokenbuild Form token generation
WriteHTML Static Cache writes

Extended Behavior has

Agentcheck Agent Detection
Browsercheck Browser detection, anti-refresh
Checkactionroute Operational Route Detection
Checklang Language detection, and automatic loading of language packs
Cronrun Automatic tasks
Fireshowpagetrace Output trace to Firefox's Firebug
Robotcheck Robot detection (is to detect Baidu spiders, etc.)
Upgrade Automatic escalation prompt behavior


Now write an example that everyone can understand.

In App.class.php, in the App::run method, there is a place to invoke the application initialization tag.

/**     * Shortcut method used to run the application instance Portal file     * @access public     * @return void *    /static public function run () {        //At the beginning of the project Start tag tag        (' App_init ');        App::init ();        Project start tag tag        (' App_begin ');        Session Initialization        session (C (' session_options '));        Log application initialization time        G (' inittime ');        App::exec ();        Item end tag Tag        (' App_end ');        return;    }

Now create a new AppinitBehavior.class.php in the project/lib/behavior.

<?phpclass Appinitbehavior extends behavior{    function run (& $param) {        echo ' app started ';    }}


And then in the project/conf/tags.php.

<?phpreturn Array (    ' App_init ' =>array (        ' Appinit '    ));


This way, when you run the project, the output app starts. Very convenient. If you want to modify, expand, directly modify the class or extension class, do not need to modify the source code, extensibility is very good.

Personal qq:2387813033, (Deanne Lei) QQ Group: 252799167

This article is from the "Deanne Lei" blog, please be sure to keep this source http://a3147972.blog.51cto.com/2366547/1413619

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.