Laravel class Auth


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';
    }
}

返回"auth"到底返回给调用者的是什么?它是文本"auth"还是对象?为什么他们在那个类中只有一个方法?对不起,我只是在学oop。

提前谢谢。

在本例中,当您看到方法getFacadeAccessor时,它正在返回auth字符串。

立面只是使用其他类的"捷径",但事实上,如果不需要的话,你不应该到处使用它们

在Laravel中,您可以将对象/类绑定到应用程序中。所以你可以写例如:

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

假设SomeObject类中存在方法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,因为此方法正在返回请求的对象。