Home >PHP Framework >ThinkPHP >Completely master thinkphp's event binding, monitoring and subscription

Completely master thinkphp's event binding, monitoring and subscription

WBOY
WBOYforward
2022-04-13 12:56:383709browse

This article brings you relevant knowledge about thinkphp, which mainly introduces issues related to event binding, monitoring, and subscription. The advantage of events compared to middleware is that events are better than middleware For more precise positioning, let’s take a look at it below. I hope it will be helpful to everyone.

Completely master thinkphp's event binding, monitoring and subscription

## Recommended study: "

PHP Video Tutorial"

What is the event

The advantage of events compared to middleware is that events are more accurately positioned (or more granular) than middleware, and are more suitable for the expansion of some business scenarios. For example, we usually encounter users who need to perform a series of operations after registering or logging in. Through the event system, we can complete the login operation expansion without invading the original code, reducing the coupling of the system while still meeting business needs.

Usage of events in TP6

Explanation on the official website, there are no examples. Here I simply tell you how to use it, and you will find more operations after using it

1. Event listening

    Command line to generate listening
  • php think make:listener UserLogin
It is generally recommended to directly create the event definition file (

event.php) defines the monitoring of the corresponding event.

return [
    'bind'    =>    [
        'UserLogin' => 'app\event\UserLogin',
        // 更多事件绑定
    ],
    'listen'  =>    [
        'UserLogin'    =>    ['app\listener\UserLogin'],
        // 更多事件监听
    ],
];

2. Event subscription

    Command line
  • php think make:subscribe User will generate app\subscribe\ by default User class, or you can specify the full class name to generate.
<?php
namespace app\subscribe;

class User
{
    public function onUserLogin($user)
    {
        // UserLogin事件响应处理
    }

    public function onUserLogout($user)
    {
        // UserLogout事件响应处理
    }
}
3. Custom subscription

If you want to customize the subscription method (or method specification), you can define the subscribe method implementation.

<?php
namespace app\subscribe;

use think\Event;

class User
{
    public function onUserLogin($user)
    {
        // UserLogin事件响应处理
    }

    public function onUserLogout($user)
    {
        // UserLogout事件响应处理
    }

    public function subscribe(Event $event)
    {
        $event->listen('UserLogin', [$this,'onUserLogin']);
        $event->listen('UserLogout',[$this,'onUserLogout']);
    }
}
Then register event subscribers in the event definition file

return [
    'bind'    =>    [
        'UserLogin' => 'app\event\UserLogin',
        // 更多事件绑定
    ],
    'listen'  =>    [
        'UserLogin'    =>    ['app\listener\UserLogin'],
        // 更多事件监听
    ],
    'subscribe'    =>    [
       'app\subscribe\User',
        // 更多事件订阅
    ],
];

Event trigger
// 触发UserLogin事件 用于执行用户登录后的一系列操作
Event::trigger('UserLogin');
或者使用助手函数
event('UserLogin');

Recommended learning: "

PHP Video Tutorial

The above is the detailed content of Completely master thinkphp's event binding, monitoring and subscription. 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