Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

listen to an event in a package of laravel 5.3

Tags:

php

laravel

whats the correct way to listen to an event in a package in laravel ?

Inside the package service provider class StudentServiceServiceProvider extends ServiceProvider I have this

protected $listen = [
        \Illuminate\Auth\Events\Login::class => [
            MyListener::class,
        ],
    ];

what am i missing?

like image 450
dev1234 Avatar asked Sep 14 '25 16:09

dev1234


1 Answers

The $listen mapping is only used to register listeners at an app-level inside App\Providers\EventServiceProvider, not within a package's ServiceProvider.

The proper way to register an event listener from within a package's ServiceProvier in my opinion is to simply use the Event facade in it's boot function.

So in OP's case that would be:

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        \Illuminate\Support\Facades\Event::listen(
            \Illuminate\Auth\Events\Login::class,
            MyListener::class
        );
    }
like image 137
codeflorist Avatar answered Sep 17 '25 07:09

codeflorist