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!
What Are the Key Features of ThinkPHP's Built-in Testing Framework?Mar 18, 2025 pm 05:01 PMThe article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.
How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?Mar 18, 2025 pm 04:57 PMArticle discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.
What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?Mar 18, 2025 pm 04:54 PMThe article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges
How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?Mar 18, 2025 pm 04:51 PMThe article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]
What Are the Advanced Features of ThinkPHP's Dependency Injection Container?Mar 18, 2025 pm 04:50 PMThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159
How to Use ThinkPHP for Building Real-Time Collaboration Tools?Mar 18, 2025 pm 04:49 PMThe article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.
What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?Mar 18, 2025 pm 04:46 PMThinkPHP benefits SaaS apps with its lightweight design, MVC architecture, and extensibility. It enhances scalability, speeds development, and improves security through various features.
How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?Mar 18, 2025 pm 04:45 PMThe article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope


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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools






