为什么事件和听众不能在 laravel 9 上工作?

Why event and listeners not working on laravel 9?

提问人:Linas Lg 提问时间:11/21/2022 更新时间:11/21/2022 访问量:729

问:

被事件和听众卡住了。

EventServicePrivider:

class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        MessageSending::class => [
            MessageSendingListener::class,
        ],
        Purchased::class => [
            FacebookConversion::class,
        ],
    ];
}

事件类:

class Purchased
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $order;
    public $user;

    public function __construct(Order $order, User $user)
    {
        $this->order = $order;
        $this->user = $user;
    }

    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}

侦听器类:

class FacebookConversion
{
    public function __construct() {

    }

    public function handle(Purchased $event)
    {
        \Log::debug('not working');
    }
}

从控制器调用事件(尝试以不同的格式):

Purchased::dispatch($order, \Auth::user());
event(new Purchased($order, \Auth::user()));
Event::dispatch(new Purchased($order, \Auth::user()));

我没有收到任何错误。但没有任何记录。也许我错过了什么?谢谢。

事件 侦听器 Laravel-9

评论


答:

2赞 Linas Lg 11/21/2022 #1

哈。我只是忘记在顶部的 EventServiceProvider.php 中添加这些行:

use App\Events\Purchased;
use App\Listeners\FacebookConversion;