Home  >  Article  >  Backend Development  >  ThinkPHP behavior extension Behavior application example detailed explanation, thinkphpbehavior_PHP tutorial

ThinkPHP behavior extension Behavior application example detailed explanation, thinkphpbehavior_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:23:06888browse

Detailed explanation of ThinkPHP behavior extension Behavior application examples, thinkphpbehavior

This article introduces in detail the implementation method of ThinkPHP's behavior extension Behavior in the form of examples, which will help readers 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 ThinkPHPModecommon.php

// 行为扩展定义
'tags' => array(
'app_init'   => array(
),
'app_begin'   => array(
  'Behavior\ReadHtmlCache', // 读取静态缓存
),
'app_end'    => array(
  'Behavior\ShowPageTrace', // 页面Trace显示
),
'path_info'   => array(),
'action_begin' => array(),
'action_end'  => array(),
'view_begin'  => array(),
'view_parse'  => array(
  'Behavior\ParseTemplate', // 模板解析 支持PHP、内置模板引擎和第三方模板引擎
),
'template_filter'=> array(
  'Behavior\ContentReplace', // 模板输出替换
),
'view_filter'  => array(
  'Behavior\WriteHtmlCache', // 写入静态缓存
),
'view_end'   => array(),
),

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

// 加载模式行为定义
if(isset($mode['tags'])) {
  Hook::import(is_array($mode['tags'])?$mode['tags']:include $mode['tags']);
}

// 加载应用行为定义
if(is_file(CONF_PATH.'tags.php'))
// 允许应用增加开发模式配置定义
Hook::import(include CONF_PATH.'tags.php');  

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

ThinkPHPLibraryThinkThink.class.php will call App::run();

after initialization is completed

ThinkPHPLibraryThinkApp.class.php file is as follows:

/**
* 运行应用实例 入口文件使用的快捷方法
* @access public
* @return void
*/
static public function run() {
// 应用初始化标签
Hook::listen('app_init');
App::init();
// 应用开始标签
Hook::listen('app_begin');
// Session初始化
if(!IS_CLI){
  session(C('SESSION_OPTIONS'));
}
// 记录应用初始化时间
G('initTime');
App::exec();
// 应用结束标签
Hook::listen('app_end');
return ;
}

It can be seen that the program monitors (checks) this action through hooks before App init to see if there is anything that needs to be processed. Loop $tags['app_init'] to get the class name and automatically execute the behavior through the class name to extend the class run method

All hooks are as follows:

'url_dispatch'     // URL调度结束标签
'app_init'     // 应用初始化标签
'app_begin'     // 应用开始标签
'app_end'     // 应用结束标签
'action_begin'     // 动作执行前操作
'action_end'     // 动作执行后操作   
'ajax_return'     // 用于扩展其他返回格式数据
'path_info'       // 检测路由规则 如果没有则按默认规则调度URL
'template_filter'    // 模版编译过滤标签
'view_begin'      // 视图开始标签
'view_end'       // 视图结束标签
'view_parse'      // 视图解析标签
'view_filter'      // 内容过滤标签

The disadvantages are as follows:

1. The order is uncontrollable (the configuration file does not have special parameters to control the order). For example, when app_init has two monitors at the same time, which method is called first.

2. Monitoring is not global monitoring. The internal writing is too rigid. There are only some defined hooks that cannot automatically control each operation through configuration files (maybe they were not added due to performance considerations)

The advantages are as follows:

1. A lot of behavioral extensions can be implemented

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

Summary:

Behavior extension is to additionally perform a specific function during a certain operation of the program. For example, when the program is operating the database and reading, it obtains performance information through explian and monitors performance bottlenecks. If the time to obtain data exceeds a specific number of seconds, the relevant information will be emailed to the project manager, etc.

What are the new Widgets and Behaviors under Lib in thinkphp30 mainly used for?

Widget extension is used to output different content on the page as needed. The definition of Widget extension is to define the Widget class library under the Lib\Widget directory of the project. For details, see ThinkPHP3.0 Complete Development Manual 13.6 Widget Extension
Behavior It is an application behavior class library. Behavior extension first defines the behavior class, and then adds a certain label position. The built-in behavior extension is a good example of extension. The behavior extension class can inherit the built-in behavior base class Behavior, and use the B method to call or automatically load it. For details, see ThinkPHP3.0 Complete Development Manual 13.1.3 Behavior Extension.

How do thinkPHP extension classes inherit from Action

The method is very simple
Inherit the Action class with the class you wrote, and then inherit the class you wrote in the controller

Technical support: Thesis Area 9, providing the best for graduates Graduation work!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/844128.htmlTechArticleDetailed explanation of ThinkPHP behavior extension Behavior application examples, thinkphpbehavior This article introduces the implementation of ThinkPHP behavior extension Behavior in detail in the form of examples Methods help readers grasp more flexibly...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn