ThinkPHP Behavior extension Behavior application example _ php instance

Source: Internet
Author: User
This article mainly introduces the ThinkPHP Behavior extension Behavior application instance, which is helpful for readers to have a deep understanding of ThinkPHP framework program design, for more information about ThinkPHP Behavior extension Behavior, see the following example:

ThinkPHP Behavior extension (Behavior) process:

First, read the configuration file information:

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

Read the 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 supports PHP, built-in template engine, and third-party template engine ), 'template _ filter' => array ('behavior \ ContentReplace ', // replace template output), 'view _ filter' => array ('behavior \ WriteHtmlCache ', // write to static cache), 'view _ end' => array (),),

By default, system behavior extensions are called: static cache reading, page Trace display output, template parsing, template content output replacement, and static cache writing.

// Load mode behavior definition if (isset ($ mode ['tags']) {Hook: import (is_array ($ mode ['tags'])? $ Mode ['tags']: include $ mode ['tags']);} // load the application behavior definition if (is_file (CONF_PATH. 'tags. php ') // allows the application to add the development mode configuration definition Hook: import (include CONF_PATH. 'tags. php ');

Use hooks to load system behaviors and custom behaviors respectively, and save the configuration information to the Hook private attribute $ tags.

ThinkPHP \ Library \ Think. class. php will call App: run ();

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

/*** Shortcut used to run the app instance entry file * @ access public * @ return void */static public function run () {// app initialization tag Hook :: listen ('app _ init '); app: init (); // App start tag Hook: listen ('app _ begin'); // Session Initialization if (! IS_CLI) {session (C ('session _ options');} // record the application initialization time G ('inittime'); App: exec (); // app end tag Hook: listen ('app _ end'); return ;}

You can see whether the program needs to handle this action by using a hook before App init. Loop $ tags ['app _ init '] To get the class name and use the class name to automatically execute the behavior extension class run Method

All hooks are as follows:

'Url _ dispatch '// url scheduling end tag 'app _ init' // app initialization tag 'app _ begin' // app start tag 'app _ end' // app end tag 'Action _ begin' // operation 'Action _ end' before the action is executed // operation 'ajax _ call' after the action is executed // This tag is used to extend the data in other returned formats 'path _ info '// if no routing rule is detected, use the default rule to schedule the URL 'template _ filter' // compile the filter tag 'view _ begin' // view start tag 'view _ end' // view end tag 'view _ parse' // view resolution tag 'view _ filter' // content filter tag

Disadvantages:

1. The order is uncontrollable (the configuration file does not have any special parameters to control the order). For example, app_init has two methods to call for monitoring at the same time.

2. Monitoring is not too hard to write in global monitoring. Only some defined hooks that cannot be automatically controlled by the configuration file (probably because of performance)

Advantages:

1. Many behavior extensions can be implemented.

2. Proxy detection, browser anti-Refresh detection, and operation route Detection

Summary:

Behavior extension is to execute a specific function in an additional operation of the program. For example, when the program reads data in the database, it obtains performance information through explicit and monitors performance bottlenecks. If the data is obtained more than a specified number of seconds, it sends 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: 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.