因此,我正在try 新的Laravel 5活动方法.

在我的存储库中,我启动了"KitchenStored"事件,如下所示:

//  Events
use App\Events\KitchenStored;

class EloquentKitchen implements KitchenInterface {

    public function store($input) {
        $kitchen        = new $this->kitchen;
        $kitchen->name  = $input['name'];
        $kitchen->save();

        \Event::fire(new KitchenStored($kitchen));

        return $kitchen;
    }

Which successfully fires this event:

<?php namespace App\Events;

use App\Events\Event;

use Illuminate\Queue\SerializesModels;

class KitchenStored extends Event {

    use SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($kitchen)
    {
        $this->kitchen  = $kitchen;
    }

}

但是,它不会链接到此处理程序:

<?php namespace App\Handlers\Events;

use App\Events\KitchenStored;

use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldBeQueued;

class AttachCurrentUserToKitchen {

    /**
     * Create the event handler.
     *
     * @return void
     */
    public function __construct()
    {
        dd('handler');
    }

    /**
     * Handle the event.
     *
     * @param  KitchenStored  $event
     * @return void
     */
    public function handle(KitchenStored $event)
    {
        //
    }

}

which I know because the dd('handler'); isn't fired during the request lifecycle.

I have registered the event with its listener here:

<?php namespace App\Providers;

use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider {

    /**
     * The event handler mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        App\Events\KitchenStored::class => [
            App\Handlers\Events\AttachCurrentUserToKitchen::class
        ]
    ];

    /**
     * Register any other events for your application.
     *
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
     * @return void
     */
    public function boot(DispatcherContract $events)
    {
        parent::boot($events);
        Event::listen('App\Events\KitchenStored',
                    'App\Handlers\Events\AttachCurrentUserToKitchen');
    }

}

有谁能更好地解释这个过程,这样我就可以继续使用迄今为止最干净的代码了?

Many thanks

推荐答案

In EventServiceProvider.php, include the leading \ when referencing a class using the ::class notation:

protected $listener = [
    \App\Events\KitchenStored::class => [
      \App\Handlers\Events\AttachCurrentUserToKitchen::class,
    ],
];

您还可以添加use条语句,并保持侦听器映射简短:

use App\Events\KitchenStored;
use App\Handlers\Events\AttachCurrentUserToKitchen;
...
protected $listener = [
    KitchenStored::class => [
      AttachCurrentUserToKitchen:class,
    ],
];

Or just use the string notation:

protected $listener = [
    'App\Events\KitchenStored' => [
      'App\Handlers\Events\AttachCurrentUserToKitchen',
    ],
];

Laravel相关问答推荐

try 编写一个函数,如果产品存在,则无法删除特定类别

当使用Craftable PRO时,Wysiwig是什么时候生成的?

在GitHub操作中缺少PHPStan和PHPMD输出,但在本地存在

如何使用PHP版本8.2.6安装beyondcode/laravel-websockets软件包?

无法使用 Dompdf Laravel 9 加载图像

为什么 laravel 模型会重复一组数据以及如何(如果可能)只有一组数据?

如何在 NetBeans 中添加带有点 (blade.php) 的自定义文件扩展名?

Laravel 5.3 auth 判断构造函数返回 false

用 laravel 表单确认删除?

laravel 预期响应代码 250 但得到代码530

Laravel 5 文件下载:stream() 或 download()

如何在 Laravel 中实现数组类型路由?

Auth 在 Laravel Tinker 中不起作用

如何在laravel 5.1中使用url(路由)传递多个参数

Laravel 字符串验证以允许空字符串

配置和测试 Laravel 任务调度

如何在 Laravel 5.2 中使用 OR 条件将多个参数传递给中间件

Laravel X-CSRF-Token 与 POSTMAN 不匹配

如何判断是否连接到 Laravel 4 中的数据库?

Laravel 如何在 Eloquent 模型中添加自定义函数?