Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Event Handler Not Firing

Tags:

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

like image 969
Ed Stephenson Avatar asked Feb 01 '15 12:02

Ed Stephenson


Video Answer


2 Answers

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',
    ],
];
like image 186
John Berberich Avatar answered Sep 27 '22 20:09

John Berberich


If you run php artisan optimize, your event handlers should start listening.

Credit to mattstauffer from the larachat slack channel for that one.

like image 29
Carter Fort Avatar answered Sep 27 '22 21:09

Carter Fort