简体   繁体   English

Laravel 5.2 Auth门面和Auth :: guard($ guard)

[英]Laravel 5.2 Auth facade and Auth::guard($guard)

I am new to Laravel. 我是Laravel的新手。 I was browsing the default authenticate middleware and I see that it is using: 我正在浏览默认的身份验证中间件,我发现它正在使用:

Auth::guard($guard)->guest()

to check if the user is a guest. 检查用户是否是来宾。

The documentation at https://laravel.com/docs/5.2/authentication#retrieving-the-authenticated-user tells that one can use: https://laravel.com/docs/5.2/authentication#retrieving-the-authenticated-user上的文档告诉我们可以使用:

Auth::check()

To figure out if a user is authenticated. 确定用户是否经过身份验证。 (I image this is opposite of guest() ? (我想这是客人()的对面吗?

I tried changing the default code to use 我尝试更改要使用的默认代码

Auth::guest()

and I am getting exactly the same result. 而且我得到了完全相同的结果。

My question is, what is the difference between having guard($guard)-> or not in this case? 我的问题是,在这种情况下, 保护($ guard) - >之间有什么区别?

A related question. 一个相关的问题。 Is guest() completely opposite of check() or are there circumstances where these may return same results? guest()是否与check()完全相反,或者是否存在可能返回相同结果的情况?

Thanks! 谢谢!

The authenticate middleware allows you to specify what type of auth guard you want to use. 通过身份验证的中间件,您可以指定要使用的auth guard类型。 Laravel 5.2 comes with two out of the box, 'web' and 'api' . Laravel 5.2带有两个开箱即用的'web''api'

The Auth facade uses the 'web' guard by default if none is specified. 如果没有指定,Auth Facade默认使用'web'保护。 So for example: Auth::user() is doing this by default: Auth::guard('web')->user() 例如: Auth::user()默认情况下这样做: Auth::guard('web')->user()

The other auth driver out of the box called 'api' . 开箱即用的另一个auth驱动程序称为“api” So for example you can call your middleware like this: $this->middleware('auth:api'); 例如,您可以像这样调用您的中间件: $this->middleware('auth:api');

This will check that the user is authenticated by api_token instead of session. 这将检查用户是否通过api_token而不是session进行身份验证。 Then you can get an instance of the user by Auth::guard('api')->user() instead of Auth::user() which is the same as Auth::guard('web')->user() 然后你可以通过Auth::guard('api')->user()而不是Auth::user()得到用户的实例,这与Auth::guard('web')->user()

You would use this if your application has an API endpoint allowed for logged in users only. 如果您的应用程序仅允许登录用户使用API​​端点,则可以使用此选项。 then a user can make a request like yourapp.com/api-method?api_token=blahblah. 然后用户可以发出像yourapp.com/api-method?api_token=blahblah这样的请求。 Then you can use the 'api' auth guard to authenticate and grab the logged in User. 然后,您可以使用'api'auth guard进行身份验证并获取登录用户。

I found this tutorial pretty useful in setting it up: http://learninglaravel.net/multiple-authentication-guard-drivers-including-api-in-laravel-52 我发现本教程在设置时非常有用: http//learninglaravel.net/multiple-authentication-guard-drivers-including-api-in-laravel-52

And to answer your second question. 并回答你的第二个问题。 Yes guest() is just the opposite of check(). 是的guest()与check()正好相反。 check out laravel\\framework\\src\\Illuminate\\Auth\\GuardHelpers.php 看看laravel \\ framework \\ src \\ Illuminate \\ Auth \\ GuardHelpers.php

Well there is no difference - if you examine the Auth facade than you'll see that the Auth::guest() is a shorthand of the Auth::guard($guard)->guest() . 好吧没有区别 - 如果你检查Auth门面,你会发现Auth::guest()Auth::guard($guard)->guest()的简写。 Auth::check() is a: Auth::check()是一个:

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

and Auth::guest() is a opposit of Auth::check() : Auth::guest()Auth::check()的对立面:

/**
 * Determine if the current user is a guest.
 *
 * @return bool
 */
public function guest()
{
    return ! $this->check();
}

Firstable I wan to show you how Auth::guard()->guest() work. Firstable我想向您展示Auth :: guard() - > guest()是如何工作的。

/**
 * Attempt to get the guard from the local cache.
 *
 * @param  string  $name
 * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
 */ 

public function guard($name = null)
{
    $name = $name ?: $this->getDefaultDriver();

    return isset($this->guards[$name])
                ? $this->guards[$name]
                : $this->guards[$name] = $this->resolve($name);
}

The guard Method will Return to \\Illuminate\\Contracts\\Auth\\Guard or \\Illuminate\\Contracts\\Auth\\StatefulGuard this is two types of Interface, it will help laravel select guest() or check() method. guard方法将返回\\ Illuminate \\ Contracts \\ Auth \\ Guard或\\ Illuminate \\ Contracts \\ Auth \\ StatefulGuard这是两种类型的接口,它将帮助laravel选择guest()或check()方法。 If you have been used Auth:check() and Auth::guest() but it only show the same Result. 如果您使用过Auth:check()和Auth :: guest(),但它只显示相同的Result。 Maybe you were Set the attemp Method in login() like this code below 也许你在login()中设置了尝试方法,如下面的代码所示

Auth::attempt(['email'=>$email,'password'=>$password],$rememberMe,true)

You just need to Remove true Argument, It will Return right Value like you desire 你只需要删除真正的参数,它会像你想要的那样返回正确的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM