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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37171269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:06:19  来源:igfitidea点击:

Laravel 5.2 Auth facade and Auth::guard($guard)

phplaravelauthentication

提问by Evren Yurtesen

I am new to Laravel. I was browsing the default authenticate middleware and I see that it is using:

我是 Laravel 的新手。我正在浏览默认的身份验证中间件,我看到它正在使用:

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-usertells 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($guard)->与否有什么区别?

A related question. Is guest() completely opposite of check() or are there circumstances where these may return same results?

一个相关的问题。guest() 与 check() 完全相反还是在某些情况下它们可能返回相同的结果?

Thanks!

谢谢!

回答by Wesley Agena

The authenticate middleware allows you to specify what type of auth guard you want to use. 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. So for example: Auth::user()is doing this by default: Auth::guard('web')->user()

如果未指定,则 Auth 门面默认使用“web”防护。例如: Auth::user()默认情况下这样做:Auth::guard('web')->user()

The other auth driver out of the box called 'api'. So for example you can call your middleware like this: $this->middleware('auth:api');

另一个开箱即用身份验证驱动程序称为'api'。例如,您可以像这样调用中间件:$this->middleware('auth:api');

This will check that the user is authenticated by api_token instead of 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()

这将检查用户是否通过 api_token 而不是 session 进行身份验证。然后你可以得到一个用户的实例,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. then a user can make a request like yourapp.com/api-method?api_token=blahblah. Then you can use the 'api' auth guard to authenticate and grab the logged in User.

如果您的应用程序具有仅允许登录用户使用的 API 端点,您将使用它。然后用户可以发出类似 yourapp.com/api-method?api_token=blahblah 的请求。然后你可以使用'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-include-api-in-laravel-52

And to answer your second question. Yes guest() is just the opposite of check(). check out laravel\framework\src\Illuminate\Auth\GuardHelpers.php

并回答你的第二个问题。是的 guest() 与 check() 正好相反。查看 laravel\framework\src\Illuminate\Auth\GuardHelpers.php

回答by Filip Koblański

Well there is no difference - if you examine the Authfacade than you'll see that the Auth::guest()is a shorthand of the Auth::guard($guard)->guest(). Auth::check()is a:

好吧,没有什么区别 - 如果您检查Auth外观,您会发现 theAuth::guest()Auth::guard($guard)->guest(). 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();
}

回答by user263800

Firstable I wan to show you how Auth::guard()->guest() work.

首先我想向你展示 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. If you have been used Auth:check() and Auth::guest() but it only show the same Result. Maybe you were Set the attemp Method in login() like this code below

守卫方法会返回到\Illuminate\Contracts\Auth\Guard 或\Illuminate\Contracts\Auth\StatefulGuard 这是两种类型的接口,它会帮助laravel 选择guest() 或check() 方法。如果您使用过 Auth:check() 和 Auth::guest() 但它只显示相同的结果。也许您在 login() 中设置了 attemp 方法,如下面的代码

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

You just need to Remove true Argument, It will Return right Value like you desire

你只需要删除真正的参数,它会像你想要的那样返回正确的值