Laravel 10 How to Use Laravel's Events and Listeners for Event-Driven Architecture
Hello Artisan, today I'll show you how to use Laravel events and listeners. The event system consists of two main components: events and listeners. Events represent various occurrences or actions in your application, while listeners are responsible for handling those events. So, let's see how we can use events and listeners for event-driven architecture.
Laravel provides an artisan command for creating events. So, let's fire the below command in the terminal.
php artisan make:event OrderPlaced
It'll create a file called OrderPlaced.php under app/Events, open the file and replace with the below codes.
<?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 OrderPlaced
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $orderId;
public function __construct($orderId)
{
$this->orderId = $orderId;
}
//right now we dont need any
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
Here we'll use an artisan command for creating listener. So, let's fire the below command in the terminal.
php artisan make:listener OrderSend
It'll create a file called OrderSend.php under app/Listeners, open the file and replace with the below codes.
<?php
namespace App\Listeners;
use App\Events\OrderPlaced;
class OrderSend
{
public function __construct()
{
//
}
public function handle(OrderPlaced $event)
{
$orderId = $event->orderId;
// Perform other operations like sending confirmation emails, notifications, etc
}
}
Now we need to register our event and listener to our EventServiceProvider.php. We can also create a service provider and registers them there. But as of now we'll register it in our default event service provider. Look at the below source code
<?php
namespace App\Providers;
use App\Events\OrderPlaced;
use App\Listeners\OrderSend;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
OrderPlaced::class => [
OrderSend::class,
],
];
public function boot()
{
//
}
}
Now we'll show how we can dispatch our newly created event. So, as of now we'll use it in our controller. So, at first create a controller by firing the below command.
php artisan make:controller OrderController
It'll create a file called OrderController.php under app/Http/Controller, open the file and replace with the below codes.
<?php
namespace App\Http\Controllers;
use App\Events\OrderPlaced;
class OrderController extends Controller
{
public function placeOrder()
{
// Place the order and retrieve the order ID
$orderId = 123;
// Dispatch the event
event(new OrderPlaced($orderId));
// Continue with other actions
}
}
And that's it. You can use Event-Driven Architecture like that.
That's it for today. I hope it'll be helpful in upcoming project. Thanks for reading. 🙂