0
0
Laravelframework~10 mins

Why event-driven architecture decouples code in Laravel - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to dispatch an event in Laravel.

Laravel
<?php

use App\Events\UserRegistered;

function registerUser($user) {
    // Dispatch the event
    event(new [1]($user));
}
Drag options to blanks, or click blank then click option'
AUserRegistered
BUserListener
CUserController
DUserModel
Attempts:
3 left
💡 Hint
Common Mistakes
Using a listener or controller class instead of the event class.
Not using the 'new' keyword before the event class.
2fill in blank
medium

Complete the code to listen for the UserRegistered event in Laravel.

Laravel
<?php

namespace App\Listeners;

use App\Events\UserRegistered;

class SendWelcomeEmail
{
    public function handle([1] $event)
    {
        // Send email logic here
    }
}
Drag options to blanks, or click blank then click option'
AUserRegistered
BUserListener
CUserController
DUserModel
Attempts:
3 left
💡 Hint
Common Mistakes
Using the listener class name as the parameter type.
Omitting the type hint.
3fill in blank
hard

Fix the error in the event listener registration in Laravel's EventServiceProvider.

Laravel
protected $listen = [
    [1]::class => [
        SendWelcomeEmail::class,
    ],
];
Drag options to blanks, or click blank then click option'
AUserListener
BUserRegistered
CUserController
DUserModel
Attempts:
3 left
💡 Hint
Common Mistakes
Using the listener class as the key instead of the event class.
Using a controller or model class instead of the event class.
4fill in blank
hard

Fill both blanks to create a listener that handles the event and logs a message.

Laravel
<?php

namespace App\Listeners;

use App\Events\UserRegistered;
use Illuminate\Support\Facades\Log;

class LogUserRegistration
{
    public function handle([1] $event)
    {
        Log::[2]('User registered: ' . $event->user->email);
    }
}
Drag options to blanks, or click blank then click option'
AUserRegistered
Binfo
Cerror
DUserListener
Attempts:
3 left
💡 Hint
Common Mistakes
Using the listener class as the parameter type.
Using Log::error instead of Log::info for normal logs.
5fill in blank
hard

Fill all three blanks to create an event-driven flow that dispatches an event, listens, and handles it.

Laravel
<?php

// Dispatch event
function registerUser($user) {
    event(new [1]($user));
}

// Listener handle method
class SendNotification
{
    public function handle([2] $event)
    {
        // Notify user
        notify($event->user);
    }
}

// EventServiceProvider mapping
protected $listen = [
    [3]::class => [
        SendNotification::class,
    ],
];
Drag options to blanks, or click blank then click option'
AUserRegistered
DUserListener
Attempts:
3 left
💡 Hint
Common Mistakes
Using different class names in dispatch, handle, and mapping.
Using listener class name instead of event class.