thinkphp Behavior Extension Behavior Application Example _php example

Source: Internet
Author: User

In this paper, the implementation of Thinkphp's behavior extension behavior is introduced in detail in the form of examples, which will help readers to master the development of thinkphp in a more flexible way, and the following 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

Behavioral extensions define
' 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 invoked by default: static cache reads, page trace display output, template resolution, template content output substitution, static cache write

Load mode behavior Definition
if (isset ($mode [' tags '])) {
  hook::import ($mode [' tags '])? $mode [' Tags ']:include $mode [' tags ']);

Load application Behavior Definition
if (is_file (conf_path. ' tags.php '))
//Allow application to add Development mode configuration definition
hook::import (include Conf_ PATH. ' tags.php ');  

Use hook to load system behavior and custom behavior, and save configuration information to hook private attribute $tags

App::run () is invoked when the thinkphp\library\think\think.class.php initialization is complete;

thinkphp\library\think\app.class.php files are as follows:

* Shortcut method for running application instance Portal file
* @access public
* @return void/
static public
function run () {
//application First Start label
hook::listen (' App_init ');
App::init ();
Application Start label
hook::listen (' App_begin ');
Session Initialization
if (!IS_CLI) {session
  (' Session_options ');
Record application initialization time
G (' inittime ');
App::exec ();
Apply end tag
hook::listen (' app_end ');

You can see whether the program needs to be handled when it listens (to see) the action by hook before app Init. Loop $tags[' App_init '] gets the class name and executes the behavior extension class Run method automatically through the class name

All hooks are as follows:

' Url_dispatch '     //url dispatch end tag '
app_init '/     Apply initialize tag
' app_begin '//     Apply start tag
' app_end '/     / Apply the end tag ' action_begin '///action     before operation ' action_end '//action     performed   
' Ajax_return     '// Used to extend other return format data
' Path_info '       //Detect routing rules if not, the URL
' template_filter '    /template compiled filter label ' is scheduled by default rule
View_ Begin '//view      start label '
view_end '/       View end tag
' view_parse '/      View parse label
' view_filter '      // Content Filter Label

Disadvantages are as follows:

1. Order is not controllable (configuration file does not have special parameters to control the order) such as App_init at the same time there are 2 monitoring the first call to which method.

2. Monitoring is not a global monitor it's too dead. There are only a few well-defined hooks that cannot be automatically controlled by the configuration file. The hook for each operation (may be due to performance not added)

The advantages are as follows:

1. Can achieve a lot of behavioral expansion

2. Can agent detection, browser anti-refresh detection, operation routing detection, etc.


Behavioral extensions are the extra way to perform a particular function when a program is in operation. If the program in the operation of the database read through the Explian to obtain performance information and monitoring performance bottlenecks, such as the occurrence of the acquisition of data more than a specific number of seconds to email the relevant information to the project manager.

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: 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.