This paper introduces in detail the implementation method of Thinkphp's behavior extension behavior in the form of an example, which can help the reader to master the development of thinkphp more flexibly, the steps are as follows:
thinkphp behavior Extension (Behavior) process:
The first 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
The behavior extension defines the ' 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 supports PHP, built-in template engine and third-party template engine), ' template_filter ' = = Array ( ' behavior\ Contentreplace ',//template output replacement), ' view_filter ' = = Array ( ' behavior\writehtmlcache ',//write static cache), ' View_end ' = = Array (),),
System behavior extensions are called by default: Static cache reads, page trace display output, template parsing, template content output substitution, static cache writes
The load mode behavior defines if (Isset ($mode [' tags ')]) {Hook::import ($mode [' tags ']) {is_array ") $mode [' tags ' ]:include $mode [' tags ' ]);} Load app behavior definition if (is_file (conf_path. ' tags.php '))//Allow application to add Development mode configuration definition Hook::import (include Conf_path. ' tags.php ');
Load system behavior and custom behavior with hooks, and save configuration information to hook private property $tags
App::run () is called when the thinkphp\library\think\think.class.php initialization is complete;
The thinkphp\library\think\app.class.php file is as follows:
/*** shortcut method used to run the application instance portal file * @access public* @return void*/static public Function Run () {//Apply Initialize tag hook::listen (' App_init '); App::init ();//Apply start tag hook::listen (' App_begin ');//Session Initialize if (!IS_CLI) { session (C (' session_options '));} Log application initialization time g (' inittime '); App::exec ();//Apply end tag Hook::listen (' App_end '); return;}
You can see if the program needs to be processed before the app init through the hooks to listen (see) This action. Loop $tags[' App_init '] gets the class name and automatically executes the behavior extension class using the class name Run method
All hooks are as follows:
' Url_dispatch ' //url dispatch end tag ' app_init '// Apply initialize tag ' app_begin '/ /Apply start tag ' app_end '//apply end tag ' action_ Begin ' //action before action ' action_end '//action after Operation ' Ajax_return '/ /For extending other return format data ' path_info ' //Detect routing Rules If no then the default rules schedule URL ' template_filter ' //Template compilation Filter label ' View_begin '// View start tag ' view_end '/ /view end tag ' View_parse ' // View Resolution label ' View_filter ' //Content Filter Tag
Disadvantages are as follows:
1. The order is not controlled (the configuration file does not have a specific parameter to control the order), such as the App_init also have 2 monitoring when the first call which method.
2. Monitoring is not a global monitoring internal write too dead only some of the definitions can not be automatically controlled by the configuration file for each operation of the hook (probably considering the performance is not added)
The advantages are as follows:
1. A lot of behavioral extensions can be achieved
2. Can agent detection, browser anti-refresh detection, operational routing detection, etc.
Summarize:
A behavior extension is an extra function that is performed when a program is operating. If the program in the operation of the database read the performance information through the Explian and monitor performance bottlenecks, such as the acquisition of data more than a specific number of seconds to email the relevant information to the project manager.