检查Laravel控制器中的多重防护

时间:2018-04-17 06:59:52

标签: php laravel authentication laravel-5 guard

我在laravel中创建了三种类型的防护,我可以检查auth用户但我们有一些路由和控制器对所有用户都是相同的我需要检查相同路由或控制器中的所有防护对于每种类型的用户记录英寸

我的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,
        ],
    ],
];

如何检查同一控制器或路线中的多个警卫?

1 个答案:

答案 0 :(得分:7)

对于要进行身份验证的每个控制器

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

OR 你可以在路线中使用

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

您可以像这样访问刀片中的用户详细信息

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

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