调度事件,是把这个事件推送给队列吗

OrderShipped::dispatch($order)

event(new OrderShipped($order))

有什么区别 是不是都是去执行一个事件?

第一种是必须用队列,
第二种可以不是队列?

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

两个没有区别
当你的事件使用了 Illuminate\Foundation\Events\Dispatchable trait
便可以直接调用 dispatch 方法。

// @see vendor/laravel/framework/src/Illuminate/Foundation/Events/Dispatchable.php
/**
 * Dispatch the event with the given arguments.
 *
 * @return void
 */
public static function dispatch()
{
    return event(new static(...func_get_args()));
}

第二种则是使用了辅助函数 event()

// @see vendor/laravel/framework/src/Illuminate/Foundation/helpers.php
if (! function_exists('event')) {
    /**
     * Dispatch an event and call the listeners.
     *
     * @param  string|object  $event
     * @param  mixed  $payload
     * @param  bool  $halt
     * @return array|null
     */
    function event(...$args)
    {
        return app('events')->dispatch(...$args);
    }
}

是否使用队列取决于你的事件监听器是否实现 Illuminate\Contracts\Queue\ShouldQueue 接口
就是你监听事件后想要做的事,具体使用请参考文档 事件系统《Laravel 9 中文文档》
当程序发现你的监听器实现了该接口,便会将其放入队列去处理

// @see vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php
/**
 * Determine if the event handler class should be queued.
 *
 * @param  string  $class
 * @return bool
 */
protected function handlerShouldBeQueued($class)
{
    try {
        return (new ReflectionClass($class))->implementsInterface(
            ShouldQueue::class
        );
    } catch (Exception $e) {
        return false;
    }
}
2年前 评论
Darkkk (楼主) 2年前
讨论数量: 4

两个没有区别
当你的事件使用了 Illuminate\Foundation\Events\Dispatchable trait
便可以直接调用 dispatch 方法。

// @see vendor/laravel/framework/src/Illuminate/Foundation/Events/Dispatchable.php
/**
 * Dispatch the event with the given arguments.
 *
 * @return void
 */
public static function dispatch()
{
    return event(new static(...func_get_args()));
}

第二种则是使用了辅助函数 event()

// @see vendor/laravel/framework/src/Illuminate/Foundation/helpers.php
if (! function_exists('event')) {
    /**
     * Dispatch an event and call the listeners.
     *
     * @param  string|object  $event
     * @param  mixed  $payload
     * @param  bool  $halt
     * @return array|null
     */
    function event(...$args)
    {
        return app('events')->dispatch(...$args);
    }
}

是否使用队列取决于你的事件监听器是否实现 Illuminate\Contracts\Queue\ShouldQueue 接口
就是你监听事件后想要做的事,具体使用请参考文档 事件系统《Laravel 9 中文文档》
当程序发现你的监听器实现了该接口,便会将其放入队列去处理

// @see vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php
/**
 * Determine if the event handler class should be queued.
 *
 * @param  string  $class
 * @return bool
 */
protected function handlerShouldBeQueued($class)
{
    try {
        return (new ReflectionClass($class))->implementsInterface(
            ShouldQueue::class
        );
    } catch (Exception $e) {
        return false;
    }
}
2年前 评论
Darkkk (楼主) 2年前

这些都是一个快捷方法

file

file

2年前 评论

这两个都是派发事件
file

file

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!