In this tutorial you will learn Laravel logout event listener example step by step. Many times we need to add or set some funcationality before process the logout (signout) from our application. So we can use event and listener to add our logic before signed out the user.
Laravel login and logout events are predefined, So we don’t need to create event we just need to define both event and listener inside the EventServiceProvider.php file.
Laravel Logout Event Listener Example
The laravel logout event is already created so we need only logout listener. So we create LogSuccessfulLogout with the following command:
php artisan make:listener LogSuccessfulLogout --event=Logout
This command generate a listener for your laravel logout listener just like below.
Now open the EventServiceProvider.php file and add the below code on it.
protected $listen = [
'Illuminate\Auth\Events\Logout' => [
'App\Listeners\LogSuccessfulLogout',
],
];
When the logout is fire the logout listener works, So ope tjhe logout listener file which is show in App\Listeners and add your logic:
namespace App\Listeners;
use Illuminate\Auth\Events\Logout;
class LogSuccessfulLogout
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param Logout $event
* @return void
*/
public function handle(Logout $event)
{
// Do your logic
}
}
That’s all guys, if you need any solutions feel freel contact us.