Laravel events and listeners system provides a simple observer implementation, allowing you to subscribe and listen for various events that occur in your application. Events are a great way to decouple various parts of your application, making it easier to maintain and extend. Here’s a brief overview of how to use Laravel events with an example.
Why Use Events?
Decoupling: Events help in decoupling various parts of your application, promoting a cleaner codebase.
Modularity: They allow different modules to react to changes without direct dependencies.
Flexibility: Events provide flexibility to add, remove, or modify behaviour without changing the core logic.
Basic Components
Event: A class that holds information about something that has happened.
Listener: A class that handles the event.
Example: User Registered Event
Let’s create an event that triggers when a user registers and a listener that sends a welcome email.
Step 1: Create the Event
First, create the event using the Artisan command:
php artisan make:event UserRegistered
This command generates a new event class in the App\Events directory.
// app/Events/UserRegistered.php namespace App\Events; use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Queue\SerializesModels; use App\Models\User; class UserRegistered { use Dispatchable, SerializesModels; public $user; public function __construct(User $user) { $this->user = $user; } }
Step 2: Create the Listener
Next, create the listener that will handle the event:
php artisan make:listener SendWelcomeEmail –event=UserRegistered
This command generates a new listener class in the App\Listeners directory.
<?php // app/Listeners/SendWelcomeEmail.php namespace App\Listeners; use App\Events\UserRegistered; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Support\Facades\Mail; use App\Mail\WelcomeEmail; class SendWelcomeEmail implements ShouldQueue { use InteractsWithQueue; public function handle(UserRegistered $event) { Mail::to($event->user->email)->send(new WelcomeEmail($event->user)); } }
Step 3: Register the Event and Listener
Now, register the event and listener in EventServiceProvider.
<?php // app/Providers/EventServiceProvider.php namespace App\Providers; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use App\Events\UserRegistered; use App\Listeners\SendWelcomeEmail; class EventServiceProvider extends ServiceProvider { protected $listen = [ UserRegistered::class => [ SendWelcomeEmail::class, ] ]; public function boot() { parent::boot(); } }
Step 4: Trigger the Event
Finally, trigger the event in your registration logic, typically in a controller.
<?php // app/Http/Controllers/Auth/RegisterController.php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use App\Providers\RouteServiceProvider; use App\Models\User; use App\Events\UserRegistered; use Illuminate\Foundation\Auth\RegistersUsers; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; class RegisterController extends Controller { use RegistersUsers; protected $redirectTo = RouteServiceProvider::HOME; protected function validator(array $data) { return Validator::make($data, [ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 'password' => ['required', 'string', 'min:8', 'confirmed'], ]); } protected function create(array $data) { $user = User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), ]); event(new UserRegistered($user)); return $user; } }
In this example, when a user registers, the User Registered event is dispatched, and the Send Welcome Email listener handles the event by sending a welcome email.
Also Read:-
Exploring the Power of the the_content() Function in WordPress
Different methods for declaration of an arrays
Also Visit:-