Home  >  Article  >  PHP Framework  >  Introducing the hook mechanism in the tp5 framework

Introducing the hook mechanism in the tp5 framework

藏色散人
藏色散人forward
2021-05-08 17:01:113070browse

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_init Application Initialize the label bit. In the initCommon function of think\App.php, Hook::listen('app_init'); does not receive parameters.

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.
  1. 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.
  2. 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.
  3. view_filter View output filter tag bit, in the fetch function of think\View.php, receives a parameter, the view string.
  4. app_end The application end tag is, in the run function in think\App.php, Hook::listen('app_end');
  5. log_write log write method tag, when calling Log: :wirte is executed before logging. Receives a parameter, the log information to be written.
  6. response_end The response end tag is. In the send function of think\Response.php, it is executed after responding to the client request.
  7. 3. The registration and monitoring of hook functions depend on think\Hook.php
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.
> 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之前执行,不然就没什么用处了。

5. Binding of hook functions.

The hook function can bind a class alone, a method in the class, or an anonymous function directly. When bound to a class, if there is a run function in the class, the run function will be executed directly. If there is no run function and there is a function with the same name as the hook, the function will be executed. (Note: The hook function is named in camel case, and the priority of the hook function name is greater than the priority of the run function)

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete