【问题标题】:Laravel class AuthLaravel 类认证
【发布时间】:2016-04-09 15:31:01
【问题描述】:

你好,我可以在 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' 究竟返回给调用者的是什么?它是文本'auth'还是一个对象?他们在该类中只有一种方法的原因是什么?抱歉,我只是在学习 oop。

提前谢谢你。

【问题讨论】:

  • 它在引号中,它将作为字符串返回。
  • 并等待返回“auth”到底返回给调用者的问题是什么?

标签: php laravel laravel-5 laravel-5.2 laravel-facade


【解决方案1】:

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

Facades 只是使用其他类的“捷径”,但事实上,如果你不需要,你不应该在任何地方使用它们。

在 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 - 因为该方法正在返回请求的对象。

【讨论】:

    猜你喜欢
    • 2014-12-18
    • 2013-02-07
    • 2021-03-05
    • 2018-04-13
    • 2017-05-08
    • 2018-01-27
    • 2021-09-10
    • 2015-07-31
    • 2021-09-11
    相关资源
    最近更新 更多