如何在Laravel中找到哪个“后卫"发出了当前请求?

编程入门 行业动态 更新时间:2024-10-10 03:33:26
本文介绍了如何在Laravel中找到哪个“后卫"发出了当前请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的auth.php:

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

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

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中找到哪个“后卫"发出了当前请求?

本文发布于:2023-11-08 20:50:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570424.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后卫   中找到   发出了   如何在   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!