ThinkPHP6 events and multiple applications
Event
1. Events are somewhat similar to middleware, except that events can more accurately locate more detailed business scenarios;
2. Events can be defined : event class, event listening class, event subscription class;
3. We first create a test event class: TestEvent.php, and manually create a test class;
public function __construct() { //注册监听器 Event::listen('TestListen', function ($param) { echo '我是监听器,我被触发了!'.$param; }); } public function info() { echo '登录前准备!'; Event::trigger('TestListen', 'ok'); //触发监听器 event('TestListen'); //助手函数触发 }
4. We also You can use the listening class to design the listener and create it using the command line;
php think make:listener TestListen
public function info() { echo '登录前准备!'; Event::listen('TestListen', TestListen::class); //这句可以定义到配置文件 Event::trigger('TestListen'); }
5. In app/event.php, listen is configured to listen to the listening class. The configuration method is as follows:
'listen' => [ 'TestListen' => [\app\listener\TestListen::class] ],
6 . When the listening class is triggered, it will automatically execute the handle() method to implement the listening function;
public function handle($event) { echo '我是监听类!'.$event; }
7. The system also has built-in system-triggered events, which will automatically trigger as long as the conditions are met;
Event description parameters AppInit application initialization tag bit None HttpRun application start tag bit None HttpEnd application end tag bit Current response object instance LogWrite log write method tag bit Currently written log information RouteLoaded Route loading completed None
8. The event listening class can monitor multiple listening classes at the same time, as long as it is bound to an identifier;
'TestListen' => [ \app\listener\TestListen::class, \app\listener\TestOne::class, \app\listener\TestTwo::class ]
9. For those who need multiple monitoring, the listening class is not flexible enough. And there will be a lot of classes created, you can use the subscription class;
10. The subscription class is to use the on method name to monitor events as an internal method;
php think make:subscribe UserSub class UserSub { public function onUserLogin(){ echo '处理登录后的监听!'; } public function onUserLogout(){ echo '处理退出后的监听!'; } }
11. Then, we go directly Register in app/event.php;
'subscribe' => [ 'UserSub' => \app\subscribe\UserSub::class, ],
12. Then, the two methods listen to the two event methods respectively, and just call the method name directly;
public function login(){ echo '登录成功!'; Event::trigger('UserLogin'); } public function logout(){ echo '退出成功!'; Event::trigger('UserLogout'); }
13. For event classes, it is very There are few scenarios where it is necessary to use it. After all, the system provides many precise solutions;
php think make:event UserEvent
class UserEvent { public function __construct() { echo '我是事件类!'; } } Event::trigger(new UserEvent());
Multi-application mode
1. Since the multi-application mode is an extension, we Additional installation is required;
composer require topthink/think-multi-app
2. After installation, create two application directory folders, index and admin;
3. Just move the controller and model in and modify the corresponding namespace;
4. Add two application directory folders, index and admin, to the view and move them into the corresponding folders;
5. The default application is index, which can be modified in app.php;
// 默认应用 'default_app' => 'index',
6. We can do application mapping, such as mapping the admin directory to think, and admin is abandoned;
// 应用映射(自动多应用模式有效) 'app_map' => [ 'think' => 'admin' ],
7. We can also do domain name binding, for example, using domain name binding in the background, Direct access;
// 域名绑定(自动多应用模式有效) 'domain_bind' => [ 'news.abc.com' => 'admin', '*' => 'index' ],
8. Route modification: The route needs to be established separately in the application directory, and the internal coding does not need to be changed;
Recommended tutorial: "ThinkPHP Tutorial"
The above is the detailed content of ThinkPHP6 events and multiple applications. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
