我怎样才能找到哪个“守卫”在Laravel中提出了当前的要求?

时间:2018-04-26 10:14:40

标签: php laravel laravel-5

这是我的auth.php

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

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

],

现在,如果useradmin都在同一个浏览器中登录,我该如何检查哪个警卫发出了请求?

请注意,无论是谁提出请求,Auth::guard('admin')->check()Auth::check()都会返回true

2 个答案:

答案 0 :(得分:2)

我假设您想知道哪个警卫auth()->user()所指-哪个auth()->guard()可以访问

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

$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);

答案 1 :(得分:0)

获取应用程序中定义的所有防护措施,并找到当前用户登录时使用的防护措施。

#!/bin/bash

# semicolon removed
count=10

# numerical version of 'for'
for (( i = 1; i <= count; i++ ))
do
    # here we use printf to get a number with at least 2 digits
    printf -v num %02d $i

    # touch is not needed, because the following line
    # will create and update timestamps too

    # create and touch file. Text is an UUID.
    uuidgen > "IN-USS-NABT-A-TATNov-$num.txt"
done