Laravel class 授权

Laravel class Auth

你好,我可以在 laravel 框架中询问这个吗

namespace Illuminate\Support\Facades;

/**
 * @see \Illuminate\Auth\AuthManager
 * @see \Illuminate\Contracts\Auth\Factory
 * @see \Illuminate\Contracts\Auth\Guard
 * @see \Illuminate\Contracts\Auth\StatefulGuard
 */
class Auth extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'auth';
    }
}

return 'auth' 对调用者的 return 到底是什么?是文本 'auth' 还是对象?他们在 class 中只有一种方法的原因是什么?抱歉我刚学oop。

提前致谢。

在这种情况下,如您所见,方法 getFacadeAccessor 它返回 auth 字符串。

Facades 只是 "shortcuts" 使用其他 classes 但实际上如果不需要的话你不应该在任何地方使用它们。

在 Laravel 中,您可以将 objects/classes 绑定到应用程序中。所以你可以这样写:

$app->bind('something', function() {
   return new SomeObject();
});

假设 SomeObject class 中有方法 doSomething

现在您可以使用此方法:

$app['something']->doSomething();

但是你也可以创建门面:

class GreatClass extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'something';
    }
}

现在您可以在应用程序的任何地方使用:

GreatClass::doSomething();

回答您的问题,此 getFacadeAccessor 仅返回绑定到应用程序时使用的对象的名称。要了解它的使用方式,您可以查看来源:

/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php

您应该首先查看的方法是 getFacadeRoot - 因为此方法返回请求的对象。