![$blog_data[0]->title](https://theeducation.net/public/frontend/assets/blog_images/639eb41dd2015image063301.jpg)
Introduction:
In this tutorial, we will learn and explore Laravel 8 events and listeners. We will develop a small demo application in which we will send an email whenever a user is created. In this scenario, we can say user creation is an event, and the listener will send an email on listening to the event.
Register Event and Listener:
The first step is to register the event and the respective listener. For that, Laravel provides an EventServiceProvider.php file where you can define events and listeners.
The $listen property contains an array for registering all the events and listeners as key-value pairs. Define events and listeners as shown below.
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
'App\Event\UserCreated' => [
'App\Listeners\SendEmail'
]
];
Once you register the event and listener, run the below command to create the files for the respective event and listener.
php artisan event:generate
The above command will generate two files, namely:
- UserCreated in app/Events
- SendEmail in app/Listeners
Once done with the Laravel 8 events and listeners setup, now it’s time to get our hands on the logic part.
Define Event Logic:
In this section, we will define the action (event) on which we want the set of logic to be performed by the listener; in our demo app, the action is creating a user.
Open App\Events\UserCreated.php and use the below code to pass the actual email address as $email to the __construct method of the UserCreated class.
// App\Events\UserCreated.php
public $email;
public function __construct($email)
{
$this->email = $email;
}
The entire file will look like this.
namespace App\Event;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class UserCreated implements ShouldQueue
{
use Dispatchable, InteractsWithSockets, SerializesModels;7i
public $email;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($email)
{
$this->email = $email;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
Here we inherit the ShouldQueue interface using class UserCreated implements ShouldQueue.It will automatically place the logic in the queue.
Define Listener Logic: handle() method:
Here, we will write actual logic for sending an email whenever the user is created. In App\Listeners\SendEmail.php, pass the UserCreated $event parameter to handle() method so that we can have a value from the event. The logic within the handle() method will be executed whenever the event is called.
public function handle(UserCreated $event)
{
print_r($event->email);
//We can send a mail from here
echo ".. From Listeners";
exit;
}
Dispatch Event:
After creating the event and listener, now it’s time to dispatch the event. It’s pretty straightforward; you just need to pass the event class object to the event() method.
// UserController.php
event (new UserCreated(“abc@gmail.com”));
0 Comments
Post Comment