Events and listener in laravel
In Laravel, events are a mechanism to implement the Observer pattern, allowing you to decouple classes and trigger actions when specific events occur. An event is dispatched, and listeners respond to it, events and listener in laravel enabling you to handle tasks like sending emails, logging, or executing other actions based on certain occurrences within the application.
Lets start how to integrated events and listener in laravel in few steps
Step:1 first you need to create a event in laravel
for create an event you need to run a command to you command prompt
this command creates an event inside app/events/SendMailNotification.php
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;class SendMailNotification
{
use Dispatchable, InteractsWithSockets, SerializesModels;/**
* Create a new event instance.
*/
public $email;
public $message;
public $subject;
public function __construct($email,$message,$subject)
{
$this->email = $email;
$this->message = $message;
$this->subject = $subject;
}/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel(‘channel-name’),
];
}
}
in event first you create the variable which you want to pass inside this in above example we define three variable email,subject and message and pass to construct.
Step:2 Create a listener to handle request of event
for create an listener you need to run a command to you command prompt
this command creates listener at app/listeners/SendMailNotificationFired.php
the code of listener below:-
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Mail;
class SendMailNotificationFired
{
/**
* Create the event listener.
*/
public function __construct()
{
//
}/**
* Handle the event.
*/
public function handle(SendMailNotification $event): void
{
//write all login and email here
dd($event->email.”.$event->message.”.$event->subject);
}
}
the function handle pass event and all variable call by event object
Step:3 create a controller to call an event
to create controller run command in command prompt
Step:4 create a route in web.php
use Illuminate\Support\Facades\Route;
use Symfony\Component\Mailer\Transport\SendmailTransportFactory;/*
|————————————————————————–
| Web Routes
|————————————————————————–
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the “web” middleware group. Make something great!
|
*/Route::get(‘/’, function () {
return view(‘welcome’);
});
Route::get(‘/send’,[SendEmailNotificationController::class,’send’]);
hit the url http://127.0.0.1:8000/send in browser
Events and listener in laravel