Laravel 5: Events – Subscribers

Scheduler: Events – Subscribers


Subscribers – multiple listening.

Subscriber is nothing but a class, that allows to define many event handlers, not just one.

Creating Subscriber

Take Laravel example:

So, you have 2 listeners.

Each one has 2 elements:

  1. event being fired, e.g.: ‘Illuminate\Auth\Events\Logout’
  2. listener dealing with even defined in #1, e.g.: ‘App\Listeners\UserEventSubscriber@onUserLogout’
Note:

Entry #2, shows after ‘at’ which method inside of Subscriber is going to do the job: ‘@onUserLogout‘.

 


Registering Subscriber

Besides internal subscriber mapping in subscribe() method, you must register it with EventServiceProvider via protected $subscribe:

Now, you are done.