Events and listener in laravel

Events and listener in laravel

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.

Events and listener in laravel

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

php artisan make:event SendMailNotification

this command creates an event inside app/events/SendMailNotification.php

namespace App\Events;use Illuminate\Broadcasting\Channel;
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

php artisan make:listener SendMailNotificationFired

this command creates listener at app/listeners/SendMailNotificationFired.php
the code of listener below:-

namespace App\Listeners;use App\Events\SendMailNotification;
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

php artisan make:controller SendEmailNotificationController
the code of controller is below
namespace App\Http\Controllers;
use App\Events\SendMailNotification;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use event;
class SendEmailNotificationController extends Controller
{
    public function send(){
        $email = ‘test@gmail.com’;
        $message = ‘test event fired’;
        $subject = ‘Event-Listener’;
        event(new SendMailNotification($email,$message,$subject));
    }
}

Step:4 create a route in web.php

use App\Http\Controllers\SendEmailNotificationController;
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

Events and listener in laravel

 

Leave a Reply

Your email address will not be published. Required fields are marked *