了解Auth :: check方法 - 它是如何工作的

时间:2017-01-14 12:55:50

标签: php laravel laravel-5

我在管理员控制器中创建了以下方法:

public function index() {
    // $recentBlogPost = DB::table('Admin')->get();
    // Auth::logout();

    if (!(Auth::check())) {
        return Redirect::to('login');
    }       

    $tags = DB::table('Tags')->get();
    /* convert Object to array */
    $tagsArray = array();
    foreach($tags as $tag) {
        $tagsArray[$tag->tag] = $tag->tag; 
    }
    $tagsArray = json_decode(json_encode($tagsArray) , TRUE);
    return view('admin.index')->with('tags' , $tagsArray);
}

现在我看到以下代码行

(Auth::check())    

我从这里的laravel文档中获取了此代码 - > https://laravel.com/docs/5.0/authentication

我真的很想知道那个类或方法真的是在引擎盖下做什么,我把这个文件拉到了引擎盖下的代码中。“

2 个答案:

答案 0 :(得分:1)

验证::校验()

// Illuminate\Auth\GuardHelpers
/**
 * Determine if the current user is authenticated.
 *
 * @return bool
 */
public function check()
{
    return ! is_null($this->user());
}

如果您想了解$this->user()

// Illuminate\Auth\SessionGuard
/**
 * Get the currently authenticated user.
 *
 * @return \Illuminate\Contracts\Auth\Authenticatable|null
 */
public function user()
{
    if ($this->loggedOut) {
        return;
    }

    // If we've already retrieved the user for the current request we can just
    // return it back immediately. We do not want to fetch the user data on
    // every call to this method because that would be tremendously slow.
    if (! is_null($this->user)) {
        return $this->user;
    }

    $id = $this->session->get($this->getName());

    // First we will try to load the user using the identifier in the session if
    // one exists. Otherwise we will check for a "remember me" cookie in this
    // request, and if one exists, attempt to retrieve the user using that.
    $user = null;

    if (! is_null($id)) {
        if ($user = $this->provider->retrieveById($id)) {
            $this->fireAuthenticatedEvent($user);
        }
    }

    // If the user is null, but we decrypt a "recaller" cookie we can attempt to
    // pull the user data on that cookie which serves as a remember cookie on
    // the application. Once we have a user we can return it to the caller.
    $recaller = $this->getRecaller();

    if (is_null($user) && ! is_null($recaller)) {
        $user = $this->getUserByRecaller($recaller);

        if ($user) {
            $this->updateSession($user->getAuthIdentifier());

            $this->fireLoginEvent($user, true);
        }
    }

    return $this->user = $user;
}

怎么回事?

  1. 您正在使用facade / helper - Illuminate\Support\Facades\Auth,但没有检查方法。
  2. Illuminate\Support\Facades\Auth扩展Illuminate\Support\Facades\Facade
  3. Illuminate\Support\Facades\Facade有一个神奇的__callStatic()方法
  4. 'li> __callStatic执行'{1}}方法'auth'实例
  5. 'auth'实例/单例在check
  6. 中注册
  7. 好的,这意味着'auth'实例是'Illuminate \ Auth \ AuthManager'
  8. 没有'check'方法,但'Illuminate \ Auth \ AuthManager'有一个神奇的Illuminate\Auth\AuthServiceProvider::registerAuthenticator()方法
  9. “li”__call执行'检查'方法在'guard'实例上
  10. Illuminate\Auth\AuthManager::__call()根据您的Illuminate\Auth\AuthManager::guard()
  11. 进入guard
  12. 默认情况下configs/auth.php - guardsession
  13. Illuminate\Auth\SessionGuard没有Illuminate\Auth\SessionGuard方法,但它使用check特征
  14. Illuminate\Auth\GuardHelpersIlluminate\Auth\GuardHelpers方法
  15. 换句话说,Auth facade:

    1. 执行门面方法
    2. 如果找不到facade方法,则执行check方法
    3. 如果未找到AuthManager方法,则执行guard(和guard trait)方法

答案 1 :(得分:0)

要了解其工作原理,最好查看check()方法源代码:

public function check()
{
    return ! is_null($this->user());
}

所以它只是检查用户实例是否存在并返回truefalse