How to get underlying class name from Facade name in Laravel

3.6k views Asked by At

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,

4

There are 4 answers

2
Mark Walet On BEST ANSWER

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.

0
Ben Swinburne On

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
1
Harry On

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.

0
Muhammad Adnan On

You can use getFacadeRoot() on the package/service to get its object:

For example, I write a helper function to get Facade object so I can use that Cart object with ease from anywhere:

function cart() {
    return \Gloudemans\Shoppingcart\Facades\Cart::getFacadeRoot();
}