本文介绍了如何在Laravel中找到哪个“后卫"发出了当前请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的auth.php:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],

],

现在,如果同时在同一浏览器中登录了useradmin,我该如何检查哪个保安提出了请求?

Now, if both a user and an admin are logged-in in the same browser, how do I check which guard made the request?

请注意,无论谁发出请求,Auth::guard('admin')->check()Auth::check()都将返回true.

Please note that both Auth::guard('admin')->check() and Auth::check() return true no matter who's making the request.

推荐答案

我假设您想知道auth()->user()指的是哪个防护-auth()->guard()

I am assuming you want to know which guard auth()->user() refers to - which is accessible by auth()->guard()

不幸的是,没有一个现成的功能可以实际获得守卫的 name .您可以使用auth()->guard()->getName(),但返回防护的会话标识符(即login_guardname_sha1hash),但如果需要,可以将其提取:

Unfortunately, there is not an existing function to actually get the name of the guard. You can use auth()->guard()->getName() but that returns the session identifier for the guard (i.e. login_guardname_sha1hash) but if you need it you can extract it:

$guard = auth()->guard(); // Retrieve the guard
$sessionName = $guard->getName(); // Retrieve the session name for the guard
// The following extracts the name of the guard by disposing of the first
// and last sections delimited by "_"
$parts = explode("_", $sessionName);
unset($parts[count($parts)-1]);
unset($parts[0]);
$guardName = implode("_",$parts);

这篇关于如何在Laravel中找到哪个“后卫"发出了当前请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 00:00