【问题标题】:How to check which guard we are currently within in laravel [duplicate]如何在laravel中检查我们当前所在的守卫[重复]
【发布时间】:2018-04-21 14:46:11
【问题描述】:

我这里有一个项目的 header.blade.php 文件的一部分。

                    @guest   
                    @else
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true">
                            {{ Auth::user()->name }} <span class="caret"></span>
                            </a> 
                             @if(condition here)
                             $profile='/myprofile';
                             $logout='/logout';
                             @else
                             $profile='/admin/myprofile';
                             $logout='/admin/logout';
                             @endif
                            <ul class="dropdown-menu">
                                  <li><a href="{{$profile}}">My Profile</a></li>
                               <li>  <a href="{{$logout}}">Log Out</a>                                   
                                </li>
                            </ul>
                              @endif
                        </li>
                    @endguest

我有 2 个守卫,即网络和管理员。我希望 $profile$logout 分别是 '/profile''/logout' && '/admin/profile''/admin/logout' 分别用于 2 名守卫。

现在我知道我可以检查每个警卫为Auth::guard('name')-&gt;check(); 但是,当两个守卫同时登录时,这样就会产生一种问题。即

     if(Auth::guard('web')->check())
{$profile="/myprofile";}
if(Auth::guard('admin')->check())
{$profile="/admin/myprofile";}

由于两者都将处于活动状态,因此 $profile 在每种情况下都将具有后一个值。 那么,有没有办法可以解决这个问题呢? 像

if(Auth::guard()=='admin'){
////////////////
}

这当然行不通。

【问题讨论】:

  • 如果两个守卫都处于活动状态,那么您要检查哪个守卫是一个偏好问题。
  • 谢谢萨普内什。干杯伙伴。

标签: php laravel guard


【解决方案1】:

我猜你必须为每个警卫处理的每个接口设置单独的标题。显然if(Auth::guard()=='admin'){ //////////////// } 不提守卫名字是无效的

【讨论】:

  • 我很久以前就知道了,但仍然感谢您的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
  • 2020-08-03
  • 1970-01-01
  • 2018-09-27
  • 1970-01-01
  • 2021-03-22
  • 1970-01-01
相关资源
最近更新 更多