I am finding it a bit difficult to understand Facades. Particularly how to find the underlying class name/location from a facade name. I have gone through the documentation but still not clear. For example, when using Auth::login()
, i found that there is no login()
method in the Auth facade.
class Auth extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'auth';
}
/**
* Register the typical authentication routes for an application.
*
* @return void
*/
public static function routes()
{
static::$app->make('router')->auth();
}
}
The Auth facades getFacadeAccessor()
method returns a string auth. But to which auth
class should i be looking at? How to resolve the actual class?
Thanks,
You can use getFacadeRoot()
For example
$object = Auth::getFacadeRoot() // Illuminate\Auth\AuthManager instance
or to get the fully qualified class name
$class = get_class(Auth::getFacadeRoot()) // 'Illuminate\Auth\AuthManager'
Also you can use the container to resolve a class by it's accessor. This is what Laravel does under the hood when resolving a Facade.
$object = resolve('auth'); // Illuminate\Auth\AuthManager instance
Somewhere in a Serviceprovider the auth
key is registered to something. For the auth
key that's in vendor/laravel/frameworksrc/Illuminate/Auth/AuthServiceProvider.php
. You can see that in the registerAuthenticator()
method, the auth
key is registered to the Illuminate\Auth\AuthManager
with a singleton pattern.
The container has several ways to bind a key to a specific class. methods like bind
and singleton
for example. Facades are just an extra class to call the main class statically from the root namespace.
If you want to check out which class is used, you can use the following code: get_class(resolve('auth'))
. Ofcourse, you can replace auth with any string you want to check.
Bonus: I think you can override this behaviour by registering your own manager in some kind of way. I would advise you to extend the normal AuthManager
and overwrite the methods that you want to see changed.
One option is to utilise the @see
annotations on the facade
/**
* @see \Illuminate\Auth\AuthManager
* @see \Illuminate\Contracts\Auth\Factory
* @see \Illuminate\Contracts\Auth\Guard
* @see \Illuminate\Contracts\Auth\StatefulGuard
*/
class Auth extends Facade
Usually the method should exist on these classes/interfaces
For example, Auth::check()
exists on \Illuminate\Contracts\Auth\Guard::check()
.
If you use an editor that allows you to follow through these definitions it can be a bit easier to traverse. Usually there's only one @see
annotation so it's pretty easy to find the class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With