【发布时间】:2018-08-12 13:45:58
【问题描述】:
Laravel 版本:5.5.*
PHP 版本:7.1.*
根据文档https://laravel.com/docs/5.5/notifications,订阅通知事件应该非常简单。我已按照文档中的步骤进行操作,但我的通知实现了 ShouldQueue 并且它们没有正确填充事件侦听器。我想知道问题是否似乎是 in the framework code。
请注意,在框架 github(链接在上面)中,new Events\NotificationSent($notifiable, $notification, $channel, $response) 仅由 sendToNotifiable 函数触发,而后者又仅由 sendNow 函数触发。 send 函数本身是这样的:
public function send($notifiables, $notification)
{
$notifiables = $this->formatNotifiables($notifiables);
if ($notification instanceof ShouldQueue) {
return $this->queueNotification($notifiables, $notification);
}
return $this->sendNow($notifiables, $notification);
}
也就是说,在我看来,如果是if ($notification instanceof ShouldQueue) { 的情况,事件不会触发,因为queueNotification 永远不会触发事件侦听器。我假设它进入队列,然后需要重新触发事件,但我认为这不会发生,因为我的 NotificationSent 侦听器没有填充来自该的 any 数据类构造函数。
事件服务提供者:
protected $listen = [
'Illuminate\Notifications\Events\NotificationSent' => [
'App\Listeners\NewNotificationListener',
],
新通知监听器:
<?php
namespace App\Listeners;
use Illuminate\Notifications\Events\NotificationSent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Jobs\SendEmailForNotifications;
use Illuminate\Support\Facades\Log;
class NewNotificationListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
public function handle(NotificationSent $event)
{
Log:info('Notification Listener:'.' '.var_dump($event));
SendEmailForNotifications::dispatch($event->notification)->delay(now()->addMinutes(10));
}
}
var_dump 这里是空的,我的日志中什么也没有,只有Notification Listener:。
所以我的问题是,为什么会这样,以及如何在我需要做的时候利用队列时有一个通知事件侦听器。是我做错了什么还是框架?
【问题讨论】:
标签: laravel events laravel-5.5