Laravel 5.0事件被忽略

时间:2016-04-21 09:10:32

标签: php laravel events handler

我正在使用Laravel 5.0(在较旧的项目中)并且忽略了事件处理程序。

  

知道我忘了什么吗?

这是App \ Providers \ EventServiceProvider

protected $listen = [
    SupplierCreated::class => [
        NotifySupplierCreated::class,
    ]
];

这是App \ Events \ SupplierCreated

class SupplierCreated extends Event {

    use SerializesModels;

    public $userId;

    /**
     * Create a new event instance.
     *
     */
    public function __construct()
    {
        dd('Event'); // This dd() is working! The other one isn't
    }

}

这是App \ Handlers \ Events \ NotifySupplierCreated

<?php namespace App\Handlers\Events;

use App\Events\SupplierCreated;

class NotifySupplierCreated {

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

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

}

这是我的客户端呼叫代码

event(new SupplierCreated($supplier));

1 个答案:

答案 0 :(得分:1)

我认为您可能遗漏了 App \ Providers \ EventServiceProvider

中的内容

您需要添加以下内容

 <?php namespace App\Providers;

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

use App\Events\SupplierWasCreated;
use App\Handlers\Events\NotifySupplierCreated;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = 
        SupplierWasCreated::class => [
            NotifySupplierCreated::class,
        ]
    ];

    /**
     * Register any other events for your application.
     *
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
     * @return void
     */
    public function boot(DispatcherContract $events)
    {
        parent::boot($events);

        //
    }
}

其他一切看起来应该可以正常工作。

我已使用您的代码实现了此活动。只有我缺少的是EventServiceProvider中的上述内容。

<?php

namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class SupplierWasCreated extends Event
{
    use SerializesModels;

    public $supplier;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($supplier) 
    {
        $this->supplier = $supplier
    }
    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return [];
    }
}

然后你的处理程序

<?php

namespace App\Handlers\Events;

use App\Events\SupplierWasCreated;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class NotifySupplierCreated {

    use InteractsWithQueue;

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

    }

     /**
     * Handle the event.
     *
     * @param  SupplierCreated  $event
     * @return void
     */
    public function handle(SupplierWasCreated $event)
    {
        dd($event);
    }

}