How to extend auth object in laravel?

by darrion.kuhn , in category: PHP Frameworks , 2 months ago

How to extend auth object in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lily , 2 months ago

@darrion.kuhn 

To extend the auth object in Laravel, you can create a new class that inherits from the Laravel IlluminateAuthAuthManager class and then register your custom auth object in the AuthProvider in the Laravel application.


Here is an example of how you can extend the auth object in Laravel:

  1. Create a new class that extends the Laravel IlluminateAuthAuthManager class:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// app/CustomAuthManager.php

namespace App;

use IlluminateAuthAuthManager;

class CustomAuthManager extends AuthManager
{
    // Add custom methods or properties here
}


  1. Register your custom auth object in the AuthProvider in your Laravel application:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// app/Providers/AuthServiceProvider.php

use AppCustomAuthManager;

public function register()
{
    $this->app->singleton('auth', function ($app) {
        return new CustomAuthManager($app);
    });
}


  1. Update the config/auth.php file to use your custom auth object:
1
2
3
// config/auth.php

'response' => AppCustomAuthManager::class,


Now, you have extended the auth object in Laravel using a custom class. You can add custom methods or properties to your custom auth object as needed.