I'm working with Laravel authentication. By default several events are fired during the authentication process. Listeners for these events are configured in EventServiceProvider.php
:
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'Illuminate\Auth\Events\Registered' => [
'App\Listeners\LogRegisteredUser',
],
'Illuminate\Auth\Events\Attempting' => [
'App\Listeners\LogAuthenticationAttempt',
],
'Illuminate\Auth\Events\Authenticated' => [
'App\Listeners\LogAuthenticated',
],
'Illuminate\Auth\Events\Login' => [
'App\Listeners\LogSuccessfulLogin',
],
];
To manually configure listeners for events, I have to use the listen
static method still inside EventServiceProvider.php
:
/**
*Register any other events for your application.
*
* @return void
*/
public function boot() {
parent::boot();
Event::listen('event.name', function($foo, $bar) {
//
});
}
Problem: I want to configure a manual listener for login events instead of using the $listen
array and create a session variable in response to successful logins like this:
public function boot() {
parent::boot();
Event::listen('event.login', function($guard, $user, $remember) {
session(['notification' => 'login successful']);
});
}
When I do this, the login event is never caught after a successful login.
But if I use a wildcard to set the name of the event like this:
public function boot() {
parent::boot();
Event::listen('*', function($eventName, array $data) {
session(['notification' => 'login successful']);
});
}
the listener successfully fires, however for every possible event.
Question: How do i get the names of inbuilt laravel events and how do I create a listener manually for inbuilt events (E.g. authentication events like login) inside the boot
method of EventServiceProvider.php
, without including them in the $listen
array? The reason I want to do this is so I do not generate any extra files using php artisan event:generate
command