php 检查登录了哪个 `guard`

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38999725/
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:47:04  来源:igfitidea点击:

Checking which `guard` is loggedin

phplaravellaravel-5laravel-bladelaravel-authentication

提问by Miguel

I have a multiauth laravel 5.2 app, with the fallowing guards defined on config/auth.php:

我有一个 multiauth laravel 5.2 应用程序,在以下位置定义了休闲守卫config/auth.php

...
'admin' => [
    'driver' => 'session',
    'provider' => 'admin',
],
'user' => [
    'driver' => 'session',
    'provider' => 'user',
],
...

So, adminand user.

所以,adminuser

The problem resides in the view layer, since this two loggedin guards share some views, ex:

问题出在视图层,因为这两个登录的守卫共享一些视图,例如:

Hello {{Auth::guard('admin')->user()->name}}

In this case the guard is hardcoded into the view to be always admin(it gives error when loggedin guard is user), but, to avoid have to do another equal view just for this little change, I would like have it dinamic, something like:

在这种情况下,守卫被硬编码到视图中admin(当登录守卫是时它会出错user),但是,为了避免为了这个小小的改变而不得不做另一个平等的观点,我希望它是动态的,比如:

Hello {{Auth::guard(<LOGGEDIN GUARD>)->user()->name}}

PS: I know that this could be achieved getting the corresponding url segment, ex: www.site.com/pt/user/dasboardwhich in the case it would be segment 2, but this way the app would lose scalability, since in the future the corresponding segment may not be the same (2 in the example above)

PS:我知道这可以通过获取相应的 url 段来实现,例如:www.site.com/pt/user/dasboard在这种情况下它将是段 2,但这样应用程序将失去可扩展性,因为将来相应的段可能不相同(2在上面的例子中)

回答by tomfrio

One way to do this is to extend the Laravel authentication class in the IoC container to include, for instance, a name()method that check which guard is used for the current session, and calls user()on that Guard instance.

一种方法是在 IoC 容器中扩展 Laravel 身份验证类,以包括例如name()检查当前会话使用哪个守卫的方法,并调用user()该守卫实例。

Another way is to simply use an if-statementin your Blade template:

另一种方法是在 Blade 模板中简单地使用if 语句

@if(Auth::guard('admin')->check())
    Hello {{Auth::guard('admin')->user()->name}}
@elseif(Auth::guard('user')->check())
    Hello {{Auth::guard('user')->user()->name}}
@endif

However, this is a little dirty. You can clean this up a bit by using a partial, or by passing the view a variable containing the guard name, either directly from your Controller, or via a ViewComposer, and then doing:

然而,这有点脏。您可以通过使用部分或通过直接从您的控制器或通过 ViewComposer 向视图传递包含守卫名称的变量来稍微清理一下,然后执行以下操作:

Hello {{Auth::guard($guardName)->user()->name}}

in your View.

在您的视图中。

Extending Laravel's authentication is your best option, imo.

扩展 Laravel 的身份验证是您最好的选择,imo。

回答by miken32

Since Laravel 5.5, this is easy to do with the @authtemplate directive.

从 Laravel 5.5 开始,使用@auth模板指令很容易做到这一点。

@auth("user")
    You're a user!
@endauth

@auth("admin")
    You're an administrator!
@endauth

@guest
    You're not logged in!
@endguest

Reference: https://laravel.com/docs/5.6/blade#if-statements

参考:https: //laravel.com/docs/5.6/blade#if-statements

回答by lewis4u

This will get the guard name that is used for current logged in user

这将获得用于当前登录用户的警卫名称

Auth::getDefaultDriver()

When you log in, by default it will get you the:

当您登录时,默认情况下它会为您提供:

'web'

Dependable through which guard you've been logged in it will get you that guard name.

可靠地通过哪个守卫登录它会给你那个守卫名字。

This is not applicable for APIs!!! Because API in laravel by default donesn't use session.

这不适用于 API !!!因为默认情况下,laravel 中的 API 不使用会话。

回答by Harat

I recommend to use global helper function like

我建议使用全局辅助函数,如

function activeGuard(){

foreach(array_keys(config('auth.guards')) as $guard){

    if(auth()->guard($guard)->check()) return $guard;

}
return null;
}

回答by Elihai David Vanunu

Depends of Harat answer, I built a Class names CustomAuth, and it give me easy access to Auth facade methods: user()and id().

取决于 Harat 的回答,我构建了一个类名 CustomAuth,它让我可以轻松访问 Auth 门面方法:user()id().

<?php

namespace App\Utils;

use Illuminate\Support\Facades\Auth;

class CustomAuth{
    static public function user(){
        return Auth::guard(static::activeGuard())->user() ?: null;
    }

    static public function id(){
        return static::user()->MID ?: null;
    }

    static private function activeGuard(){

        foreach(array_keys(config('auth.guards')) as $guard){

            if(auth()->guard($guard)->check()) return $guard;

        }
        return null;
    }
}