Reputation: 774
So I'm trying out the new Laravel 5 Event methodology.
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;
}
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.
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
Upvotes: 20
Views: 32823
Reputation: 3809
Make sure your EventServiceProvider is calling the parent register function.
public function register() {
parent::register():
}
Upvotes: 1
Reputation: 3383
For me, I had the issue where I had multiple listeners for a single event. In that case, the listeners are executed in order. However, if one of the listeners return false, then all of the others listeners are not executed.
Upvotes: 0
Reputation: 493
Don't be like me and cache your events locally to test something, then forget to clear that cache 😅
php artisan event:clear
Upvotes: 10
Reputation: 1064
This is an older question, but I came across this same issue and for me, it was because I'd added my new Event to the service provider but crucially I had not imported it. For anyone in 2020 with this issue, check that you have imported the event.
Upvotes: -1
Reputation: 746
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',
],
];
Upvotes: 32
Reputation: 149
I ran
composer dumpautoload
followed by
php artisan clear-compiled
Then my events began firing.
Upvotes: 14
Reputation: 221
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.
Upvotes: 0
Reputation: 973
If you run php artisan optimize
, your event handlers should start listening.
Credit to mattstauffer from the larachat slack channel for that one.
Upvotes: 18