The following tutorial column will introduce you to the hook mechanism in the tp5 framework. I hope it will be helpful to friends in need! 1. Official explanation
Behavior is a key extension in the ThinkPHP extension mechanism. Behavior can be called independently or bound to a tag. For listening, behavior also plays a major role in the officially proposed CBD model. It can be seen that behavior is of great significance in the ThinkPHP framework.2. TP5 comes with hooks (the tags are already embedded in the TP framework, we only need to configure and implement the corresponding functions to use them)
app_begin application start tag, in the run function of think\App.php, is executed after loading routing and request information. It will receive a parameter, which stores the scheduling information of this request.4. The system only has hook functions, which we can use in application\tags. Register in php, you can also use Hook::add() to add.
- module_init module initialization tag bit, in the module function of think\App.php, is executed before loading the request controller, receives a parameter, and stores the request information.
- action_init controller start tag bit, in the module function of think\App.php, after loading the controller, it is executed before using reflection to request the controller. Receives a parameter, which is an array. The first element is the request controller instance object, and the second function is the name of the controller function to be executed.
- view_filter View output filter tag bit, in the fetch function of think\View.php, receives a parameter, the view string.
- app_end The application end tag is, in the run function in think\App.php, Hook::listen('app_end');
- log_write log write method tag, when calling Log: :wirte is executed before logging. Receives a parameter, the log information to be written.
- response_end The response end tag is. In the send function of think\Response.php, it is executed after responding to the client request.
- 3. The registration and monitoring of hook functions depend on think\Hook.php
> 1. 官方文档数tags.php会自动加载,但是经测试我下载的tp5不会自动加载tags.php文件,所以就手动在think\start.php文件的App::run()->send();函数之前写了一行代码用于,注册tags文件中的钩子实现。代码为 "php // 加载Hook Hook::import(require APP_PATH . '/tags.php'); " > 2. 也可以使用Hook::add和Hook::listen来添加自己的钩子函数。Hook::listen()相当于是标签位,Hook::add()则相当于是对标签位相应的实现。所以要确保Hook::add()在Hoo::listen之前执行,不然就没什么用处了。
The above is the detailed content of Introducing the hook mechanism in the tp5 framework. For more information, please follow other related articles on the PHP Chinese website!