Laravel 5 Event Handler Not Firing

前端 未结 6 974
再見小時候
再見小時候 2020-12-31 02:02

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\\         


        
相关标签:
6条回答
  • 2020-12-31 02:09

    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.

    0 讨论(0)
  • 2020-12-31 02:12

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

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

    0 讨论(0)
  • 2020-12-31 02:13

    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',
        ],
    ];
    
    0 讨论(0)
  • 2020-12-31 02:23

    I ran

    composer dumpautoload
    

    followed by

    php artisan clear-compiled
    

    Then my events began firing.

    0 讨论(0)
  • 2020-12-31 02:25

    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.

    0 讨论(0)
  • 2020-12-31 02:36

    Don't be like me and cache your events locally to test something, then forget to clear that cache

    0 讨论(0)
提交回复
热议问题