ThinkPHP Behavior Extension Behavior Application Example Detailed Explanation

  • 2021-07-09 07:43:46
  • OfStack

This paper introduces the implementation method of ThinkPHP behavior extension Behavior in detail in the form of examples, which is helpful for readers to master the development of ThinkPHP more flexibly. The specific steps are as follows:

ThinkPHP Behavior Extension (Behavior) Process:

The first step is to read the configuration file information:


$mode = include is_file(CONF_PATH.'core.php')?CONF_PATH.'core.php':MODE_PATH.APP_MODE.'.php';

Read configuration file information ThinkPHP\ Mode\ common.php


//  Behavior extension definition 
'tags' => array(
'app_init'   => array(
),
'app_begin'   => array(
  'Behavior\ReadHtmlCache', //  Read static cache 
),
'app_end'    => array(
  'Behavior\ShowPageTrace', //  Page Trace Display 
),
'path_info'   => array(),
'action_begin' => array(),
'action_end'  => array(),
'view_begin'  => array(),
'view_parse'  => array(
  'Behavior\ParseTemplate', //  Template parsing   Support PHP Built-in template engine and 3 Square template engine 
),
'template_filter'=> array(
  'Behavior\ContentReplace', //  Template output replacement 
),
'view_filter'  => array(
  'Behavior\WriteHtmlCache', //  Write to static cache 
),
'view_end'   => array(),
),

System behavior extensions are invoked by default: static cache reading, page Trace display output, template parsing, template content output replacement, static cache writing


//  Load pattern behavior definition 
if(isset($mode['tags'])) {
  Hook::import(is_array($mode['tags'])?$mode['tags']:include $mode['tags']);
}

//  Load application behavior definition 
if(is_file(CONF_PATH.'tags.php'))
//  Allow applications to add development mode configuration definitions 
Hook::import(include CONF_PATH.'tags.php');  

Use Hook to load system behavior and custom behavior respectively, and save configuration information to Hook private property $tags

ThinkPHP\ Library\ Think\ Think. class. App:: run () is called when initialization is complete;

The file ThinkPHP\ Library\ Think\ App. class. php is as follows:


/**
*  Run an application instance   Shortcuts to use for entry files 
* @access public
* @return void
*/
static public function run() {
//  Apply initialization label 
Hook::listen('app_init');
App::init();
//  Apply start tag 
Hook::listen('app_begin');
// Session Initialization 
if(!IS_CLI){
  session(C('SESSION_OPTIONS'));
}
//  Record application initialization time 
G('initTime');
App::exec();
//  Apply end tag 
Hook::listen('app_end');
return ;
}

It can be seen that the program before App init through the hook to listen (see) this action if there is any need to deal with. Loop $tags ['app_init'] Gets the class name and automatically executes the behavior extension class run method by the class name

All hooks are as follows:


'url_dispatch'     // URL End of Scheduling Tag 
'app_init'     //  Apply initialization label 
'app_begin'     //  Apply start tag 
'app_end'     //  Apply end tag 
'action_begin'     //  Action before action execution 
'action_end'     //  Action after action execution    
'ajax_return'     //  Used to extend other return format data 
'path_info'       //  Detecting routing rules   If not, schedule according to default rules URL
'template_filter'    //  Template compilation filter label 
'view_begin'      //  View start label 
'view_end'       //  End of view tag 
'view_parse'      //  View parsing label 
'view_filter'      //  Content filtering label 

The disadvantages are as follows:

1. The sequence is uncontrollable (the configuration file has no special parameters to control the sequence). For example, when app_init has two monitors at the same time, which method should be called first.

2. Monitoring is not global monitoring. Internal writing is too dead. There are only 1 well-defined hooks that cannot automatically control every 1 operation through configuration files (it may not be added because of performance considerations)

The advantages are as follows:

1. You can implement a lot of behavior extensions

2. Proxy detection, browser refresh prevention detection, operation route detection and so on

Summary:

Behavior extension is to perform a specific function when a program operates. For example, when the program operates the database to read, it obtains performance information through explian and monitors performance bottlenecks. If the data is obtained for more than a specific number of seconds, it will give relevant information email to the project manager.


Related articles: