gpt4 book ai didi

php - 在 Laravel Controller 中检查多个守卫

转载 作者:可可西里 更新时间:2023-11-01 13:46:43 27 4
gpt4 key购买 nike

我在 laravel 中创建了三种类型的守卫,我可以检查 auth 用户,但是我们有一些对所有用户都相同的路由和 Controller ,我需要检查相同路由中的所有守卫或为登录的每种类型的用户检查 Controller 中。

我的 auth.php 文件看起来像这样

<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'student' => [
'driver' => 'session',
'provider' => 'students',
],
'finance' => [
'driver' => 'session',
'provider' => 'finances',
],

'admin-api' => [
'driver' => 'token',
'provider' => 'admins',
],
'finance-api' => [
'driver' => 'token',
'provider' => 'finances',
],
],
'providers' => [
'students' => [
'driver' => 'eloquent',
'model' => App\student::class,
],
'finances' => [
'driver' => 'eloquent',
'model' => App\Finance::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\users::class,
],
'finance' => [
'driver' => 'eloquent',
'model' => App\users::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'students' => [
'provider' => 'students',
'table' => 'password_resets',
'expire' => 60,
],
'finances' => [
'provider' => 'finances',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 15,
],
'finance' => [
'provider' => 'finance',
'table' => 'password_resets',
'expire' => 15,
],
],
];

如何检查同一 Controller 或路由中的多个守卫?

最佳答案

对于每个你想认证的 Controller

public function __construct()
{
$this->middleware('auth:admin,student,finance');
}

或你可以在 route 使用

Route::group(['middleware' => 'auth:admin,finance,student'], function () {
any same route here
});

你可以像这样访问 blade 中的用户详细信息

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

我希望这对某些人有帮助。

关于php - 在 Laravel Controller 中检查多个守卫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49871765/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com