Table of Contents
app_init Application Initialize the label bit. In the initCommon function of think\App.php, Hook::listen('app_init'); does not receive parameters.
5. Binding of hook functions.
Home PHP Framework ThinkPHP Introducing the hook mechanism in the tp5 framework

Introducing the hook mechanism in the tp5 framework

May 08, 2021 pm 05:01 PM
tp5

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之前执行,不然就没什么用处了。
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Clair Obscur: Expedition 33 - How To Get Perfect Chroma Catalysts
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1676
14
PHP Tutorial
1278
29
C# Tutorial
1257
24