php Laravel 5 事件处理程序未触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28262605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 Event Handler Not Firing
提问by Ed Stephenson
So I'm trying out the new Laravel 5 Event methodology.
所以我正在尝试新的 Laravel 5 事件方法。
In my repository, I'm firing the event "KitchenStored" as so:
在我的存储库中,我正在触发事件“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;
}
}
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)
{
//
}
}
which I know because the dd('handler'); isn't fired during the request lifecycle.
我知道这是因为 dd('handler'); 在请求生命周期中不会被触发。
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?
谁能更好地解释这个过程,以便我可以继续使用迄今为止最干净的代码?
Many thanks
非常感谢
回答by John Berberich
In EventServiceProvider.php
, include the leading \
when referencing a class using the ::class
notation:
在 中EventServiceProvider.php
,\
使用::class
符号引用类时包括前导:
protected $listener = [
\App\Events\KitchenStored::class => [
\App\Handlers\Events\AttachCurrentUserToKitchen::class,
],
];
You could also add use
statements and keep your listener mappings short:
您还可以添加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',
],
];
回答by Carter Fort
If you run php artisan optimize
, your event handlers should start listening.
如果您运行php artisan optimize
,您的事件处理程序应该开始侦听。
Credit to mattstauffer from the larachat slack channel for that one.
感谢来自 larachat slack 频道的 mattstauffer。
回答by jb360
I ran
我跑了
composer dumpautoload
followed by
其次是
php artisan clear-compiled
Then my events began firing.
然后我的事件开始发生。
回答by kururin
I had a similar issue and I fixed it by deleting the vendor\compiled.php file. Then I run "composer update" again and now the handler is firing as expected.
我有一个类似的问题,我通过删除 vendor\compiled.php 文件来修复它。然后我再次运行“composer update”,现在处理程序按预期触发。