Laravel 5事件处理器不烧 [英] Laravel 5 Event Handler Not Firing

查看:153
本文介绍了Laravel 5事件处理器不烧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试新的Laravel 5事件方法。

So I'm trying out the new Laravel 5 Event methodology.

在我的存储库中,我正在触发KitchenStored事件:

In my repository, I'm firing the event "KitchenStored" as so:

//  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;
    }

成功触发此活动:

<?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;
    }

}

但是,它没有链接直到这个处理程序:

However, it doesn't link up to this handler:

<?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)
    {
        //
    }

}

我知道,因为DD( '处理');在请求生命周期中没有触发。

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');
    }

}

任何人都可以更好地解释这个过程我可以继续使用我迄今为止最干净的代码?

Can anyone explain this process better so I can keep going with the cleanest code I have to date?

非常感谢

推荐答案

EventServiceProvider.php 中,包含前导 \ 当使用 :: class 符号:

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,
    ],
];

您还可以添加使用语句并保留你的收听者映射简短:

You could also add use statements and keep your listener mappings short:

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 5事件处理器不烧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆