0
0
Laravelframework~10 mins

Creating listeners in Laravel - Interactive Practice

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

Complete the code to define a listener class in Laravel.

Laravel
class UserRegisteredListener {
    public function [1]($event) {
        // handle the event
    }
}
Drag options to blanks, or click blank then click option'
Alisten
Bhandle
Cfire
Dtrigger
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'listen' or 'fire' instead of 'handle' as the method name.
2fill in blank
medium

Complete the code to register a listener for an event in Laravel's EventServiceProvider.

Laravel
protected $listen = [
    UserRegistered::class => [
        [1]::class,
    ],
];
Drag options to blanks, or click blank then click option'
AUserRegisteredListener
BUserRegisteredEvent
CUserRegisteredHandler
DUserRegisteredSubscriber
Attempts:
3 left
💡 Hint
Common Mistakes
Using the event class name instead of the listener class.
Using 'Subscriber' when the question is about listeners.
3fill in blank
hard

Fix the error in the listener method signature to properly receive the event.

Laravel
public function handle([1] $event) {
    // process event
}
Drag options to blanks, or click blank then click option'
AUserRegistered
BUserRegisteredListener
CEvent
DListener
Attempts:
3 left
💡 Hint
Common Mistakes
Using the listener class as the parameter type.
Using a generic 'Event' class without importing it.
4fill in blank
hard

Fill both blanks to dispatch an event and listen to it with a listener.

Laravel
event(new [1]($user));

protected $listen = [
    [2]::class => [
        SendWelcomeEmail::class,
    ],
];
Drag options to blanks, or click blank then click option'
AUserRegistered
BUserLoggedIn
DUserLoggedOut
Attempts:
3 left
💡 Hint
Common Mistakes
Using different event names for dispatching and listening.
Confusing login and registration events.
5fill in blank
hard

Fill all three blanks to create a listener class that handles an event and logs a message.

Laravel
use Illuminate\Support\Facades\Log;

class [1] {
    public function handle([2] $event) {
        Log::info('[3] event handled');
    }
}
Drag options to blanks, or click blank then click option'
AUserRegisteredListener
BUserRegistered
DUserLoggedIn
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching event names in the handle method and log message.
Using the wrong class name for the listener.