How to get underlying class name from Facade name in Laravel
Asked Answered
F

4

7

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,

Fenton answered 12/9, 2017 at 9:49 Comment(2)
This might be helpfull laravel.com/docs/5.5/facades#how-facades-work If you scroll a bit down you will see which facade corresponds to which classCoagulate
Brilliant question. It's been driving me nuts for a long time.Erlking
H
3

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.

Hid answered 12/9, 2017 at 10:1 Comment(2)
Hi Mark. How does the application know that to resolve 'app' it has to go to the class Illuminate/Auth/AuthServiceProvider.php?Fenton
You register the AuthServiceProvider in the config/app.php file. If you register it there, it will execute the boot and register method on startup.Hid
D
7

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
Docile answered 12/9, 2017 at 10:10 Comment(0)
H
3

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.

Hid answered 12/9, 2017 at 10:1 Comment(2)
Hi Mark. How does the application know that to resolve 'app' it has to go to the class Illuminate/Auth/AuthServiceProvider.php?Fenton
You register the AuthServiceProvider in the config/app.php file. If you register it there, it will execute the boot and register method on startup.Hid
O
2

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.

Octodecillion answered 12/9, 2017 at 10:23 Comment(1)
Hi morph. Yes sublime text has this feature. But i wanted to learn how to resolve this on my own. Thanks though.Fenton
R
0

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();
}
Radioactive answered 3/4, 2020 at 23:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.