Laravel - Event Handling

Laravel - Event Handling

 

Laravel - Event Handling


Events give a basic onlooker usage which enables a client to buy in and tune in to different events activated in the web application. All the event classes in Laravel are put away in the application/Events organizer and the audience members are put away in the application/Listeners envelope.

The craftsman order for creating events and audience members in your web application has appeared underneath −

php artisan event:generate

This command generates the events and listeners to the respective folders as discussed above.

Event Generator

Events and Listeners serve as an incredible method to decouple a web application since one event can have various audience members which are autonomous of one another. The events envelope made by the craftsman order incorporates the accompanying two files: event.php and SomeEvent.php. They have appeared here −

Event.php

php
namespace App\Events;
abstract class Event{
   //
}

As referenced above, event.php incorporates the essential meaning of class Event and calls for namespace App\Events. Kindly note that the client characterized or custom events are made in this file.

SomeEvent.php

php

namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class SomeEvent extends Event{
   use SerializesModels;
   /**
      * Create a new event instance.
      *
      * @return void
   */
   
   public function __construct() {
      //
   }
   
   /**
      * Get the channels the event should be broadcast on.
      *
      * @return array
   */
   
   public function broadcastOn() {
      return [];
   }
}

See that this file utilizes serialization for broadcasting events in a web application and that the vital parameters are additionally instated in this file.

For example, if we need to initialize an order variable in the constructor for registering an event, we can do it in the following way −

public function __construct(Order $order) {
   $this->order = $order;
}

Listeners

Audience members handle every one of the exercises referenced in an event that is being enlisted. The craftsman order event: generate makes every one of the audience members in the application/audience members catalog. The Listeners organizer incorporates a file EventListener.php which has every one of the techniques required for dealing with audience members.

EventListener.php

php

namespace App\Listeners;

use App\Events\SomeEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class EventListener{
   /**
      * Create the event listener.
      *
      * @return void
   */
   
   public function __construct() {
      //
   }

   /**
      * Handle the event.
      *
      * @param SomeEvent $event
      * @return void
   */
   
   public function handle(SomeEvent $event) {
      //
   }
}

As mentioned in the code, it includes handle function for managing various events. We can create various independent listeners that target a single event.

Reactions

Post a Comment

0 Comments

close