0
0
Laravelframework~5 mins

Creating listeners in Laravel

Choose your learning style9 modes available
Introduction

Listeners help your app respond to events automatically. They keep your code organized and easy to manage.

When you want to send a welcome email after a user registers.
When you need to log activity after a user updates their profile.
When you want to update a cache after a database change.
When you want to trigger notifications after a new order is placed.
Syntax
Laravel
<?php

namespace App\Listeners;

use App\Events\EventName;

class ListenerName
{
    /**
     * Handle the event.
     *
     * @param  EventName  $event
     * @return void
     */
    public function handle(EventName $event)
    {
        // Your code to respond to the event
    }
}

Listeners are classes that respond to events.

The handle method runs when the event happens.

Examples
This listener sends a welcome email when a user registers.
Laravel
<?php

namespace App\Listeners;

use App\Events\UserRegistered;

class SendWelcomeEmail
{
    public function handle(UserRegistered $event)
    {
        // Send welcome email to $event->user
    }
}
This listener updates inventory after an order is placed.
Laravel
<?php

namespace App\Listeners;

use App\Events\OrderPlaced;

class UpdateInventory
{
    public function handle(OrderPlaced $event)
    {
        // Update stock based on $event->order details
    }
}
This listener cleans up data when a user is deleted.
Laravel
<?php

namespace App\Listeners;

use App\Events\UserDeleted;

class CleanUpUserData
{
    public function handle(UserDeleted $event)
    {
        // Remove user data from other tables
    }
}
Sample Program

This example shows a simple event UserRegistered and a listener SendWelcomeEmail. When the event is dispatched, the listener runs and prints a message simulating sending an email.

Laravel
<?php

// Event: UserRegistered.php
namespace App\Events;

use App\Models\User;

class UserRegistered
{
    public User $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }
}

// Listener: SendWelcomeEmail.php
namespace App\Listeners;

use App\Events\UserRegistered;

class SendWelcomeEmail
{
    public function handle(UserRegistered $event)
    {
        echo "Sending welcome email to " . $event->user->email . "\n";
    }
}

// Simulate usage

// Simple User model stub
class User {
    public string $email;
    public function __construct(string $email) {
        $this->email = $email;
    }
}

// Simulate event dispatching
function dispatchEvent(object $event, object $listener) {
    $listener->handle($event);
}

// Create user
$newUser = new User("newuser@example.com");

// Create event
$userRegisteredEvent = new UserRegistered($newUser);

// Create listener
$welcomeEmailListener = new SendWelcomeEmail();

// Dispatch event to listener
dispatchEvent($userRegisteredEvent, $welcomeEmailListener);
OutputSuccess
Important Notes

Listeners keep your code clean by separating event responses.

Listeners can be queued to run later for better performance.

Remember to register your listeners in EventServiceProvider to make them work.

Summary

Listeners respond to events automatically.

They help organize code and improve app structure.

Use listeners to run code after important actions happen.