学习LaravelNotification Event Subscriber软件包

57 阅读1分钟

LaravelNotification Event Subscriber是一个简单的包,它注册了一个事件订阅者,让你在发送通知的同时轻松运行代码。

具体来说, 这个包允许你在通知发送时或发送后运行任何动作:

use Illuminate\Support\Facades\Log;
 
class UserRegisteredNotification extends Notification
{
    /* ...Notification code... */
 
    // Handlers for sending/sent events.
    public function onSending(string $channel, $response = null): void
    {
        Log::info($this::class . ' is being sent via ' . $channel);
    }
 
    public function onSent(string $channel): void
    {
        Log::info($this::class . ' has been sent via ' . $channel);
    }
}

它的工作原理是注册一个事件订阅器,监听NotificationSendingNotificationSent 事件,检查通知类是否定义了onSendingonSent 方法。