What is Listeners in Laravel?
Listeners handle each and every activity that is mentioned in the event being registered. An artisan command which is event: generate
creates all of the listeners inside the app/listeners directory. This Listeners folder holds a file namely “EventListener.php “ that has every single method required to handle listeners.
Example
EventListener.php
namespace App\Listeners;
use App\Events\SomeEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class EventListener{
public function __construct() {
//
}
public function handle(SomeEvent $event) {
//
}
}
As mentioned above in this code, it involves the handle function to manage various events. Also, we can build several independent listeners which will target one single event.