event and listners
events -> its looking on whats going in
listener- it will task for the event
event group of tasks
listener-> hearing what happening, and implement the event
in office birthday parties or some activities called as event like in laravel some activities
js events-> on key up, enter,
laraqvel events
after register -> welcome message event or register email
reset pasword-> reset wvent
pay succesfful-> payment event
Laravel Events is a built-in feature of the Laravel framework that allows you to implement event-driven architecture in your application. With Laravel Events, you can define events that occur in your application, and then write listeners to handle those events.
Events are basically a trigger that occurs in your application. For example, you may want to notify your users when a new product is added to your store. This could be an event that you define in your application. To define an event, you create a new class that extends the Illuminate\Foundation\Events\Event
class, and define any properties and methods that you need for the event.
Once you have defined your event, you can write listeners to handle the event. Listeners are classes that implement the Illuminate\Contracts\Events\Listener
interface. They define a single handle()
method that is called when the event is triggered. Inside the handle()
method, you can write code to handle the event, such as sending an email notification to your users.
To dispatch an event, you simply call the event()
function, passing in an instance of the event class that you defined. Laravel will automatically find and call all of the listeners that are registered to handle that event.
Laravel Events is a powerful feature that can help you write more modular and scalable code, by decoupling your application logic from the specific events that trigger it.
Sure, here's an example of how to use Laravel Events in your application:
Let's say you have an online store where users can place orders for products. You want to send an email notification to your admin whenever a new order is placed. You can use Laravel Events to implement this functionality.
First, you'll need to define an event for the "order placed" event. You can create a new event class by running the following command in your Laravel project:
csharpphp artisan make:event OrderPlaced
This will create a new OrderPlaced
event class in the app/Events
directory. You can define any properties and methods that you need for the event, such as the order ID and the user who placed the order:
phpnamespace App\Events;
use Illuminate\Foundation\Events\Dispatchable
;
use Illuminate\Queue\SerializesModels;
class OrderPlaced
{
use Dispatchable, SerializesModels
;
public $orderId
;
public $userId;
public function __construct($orderId, $userId)
{
$this->orderId = $orderId;
$this->userId = $userId;
}
}
Next, you'll need to define a listener for the event. You can create a new listener class by running the following command:
csharpphp artisan make:listener SendOrderNotification --event=OrderPlaced
This will create a new SendOrderNotification
listener class in the app/Listeners
directory. You can implement the handle()
method to send the email notification:
phpnamespace App\Listeners;
use App\Events\OrderPlaced;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class SendOrderNotification implements ShouldQueue
{
use InteractsWithQueue;
public function handle(OrderPlaced $event)
{
// Send email notification to admin
// You can access the event properties like $event->orderId and $event->userId
}
}
Finally, you'll need to register the listener with the event in your EventServiceProvider
class:
phpnamespace App\Providers;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
'App\Events\OrderPlaced' => [
'App\Listeners\SendOrderNotification',
],
];
}
Now, whenever a new order is placed, you can dispatch the OrderPlaced
event like this:
phpevent(new OrderPlaced($orderId, $userId));
event means a liek ajob after particula thing happened means any order placesd we will that event,it will implement by listener
event called after register of person, but work by listener
step by step procedure
php artisan make:listener WelcomeEmail
php artisan make:listener SendWelcomeEmail
app/events/
app/listeners
php artisan queue:work --queue=listener
app/providers
eventserviceprovider -> register at this place event and listener
working example
app/listeners/
eventserviceprovider
controller
Event classes stored in ______ directory.
Marked Answer : app/Events
Correct Answer : app/Events
Listener classes stored in ______ directory.
Marked Answer : app/Listeners
Correct Answer : app/Listeners
In which provider we have to register event listeners?
Marked Answer : EventServiceProvider
Correct Answer : EventServiceProvider
Which interface is used to specify that a listener should be queued?
Marked Answer : Queue
Correct Answer : ShouldQueue
By using ________ property we can specify queue connection.
Marked Answer : $queue
Correct Answer : $connection
No comments:
Post a Comment