Search code examples
phplaravellaravel-8laravel-authentication

How do attempt static method get attached to the Auth class in Laravel?


I learnt that there is a Auth::attempt() static method in Laravel.

But when I dig into the underlying source code, I could only find the method in SessionGuard.php instead of Auth.php.

How did the method get "attached" to the Auth class?


Solution

  • That is the method that attempt is being called on in a default setup as SessionGuard is what the 'web' auth guard is using. In the Facade Class Reference you will see that Auth is referring to the AuthManager, you can look at the source of that to see the dynamic call to the driver that is actually used via magic methods.

    Auth Facade -> AuthManager -> Driver -> attempt()

    The magic __callStatic method on Facade: https://github.com/laravel/framework/blob/8.x/src/Illuminate/Support/Facades/Facade.php#L253

    The magic __call method on AuthManager: https://github.com/laravel/framework/blob/8.x/src/Illuminate/Auth/AuthManager.php#L305