如何使用两个Laravel Default Guard?

时间:2016-04-08 01:50:23

标签: php authentication laravel-5.2

我正在尝试使用相同的表单登录管理员和用户,但是config / auth我只能设置一个默认值

这是我的 config / auth

'defaults' => [
    'guard' => 'web_users',
    'passwords' => 'users',
],
'guards' => [
    'web_users' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

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

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
],
'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
    'admins' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ],
],

当我使用默认的“web_users”登录用户时,我可以使用Auth :: user()方法检索他的数据,但是如果我使用默认的“web_admins”登录Admin,则无法检索任何内容。 / p>

路线:

Route::resource('log','LogController');
Route::get('logout','LogController@logout');

记录控制器

public function store(Request $request)
{

    if(Auth::guard('web_users')->attempt(['email'=>$request['email'],'password'=>$request['password']])) {
        return Redirect::to('/');
    }

    if(Auth::guard('web_admins')->attempt(['email'=>$request['email'],'password'=>$request['password']])) {
        return Redirect::to('/');
    }
}

public function logout()
{
    Auth::logout();
    return Redirect::to('log');
}

注意:对不起我的英语,我不会说得很好

1 个答案:

答案 0 :(得分:0)

尝试使用路由文件

Route::group(['middleware' => 'auth:web_users,web_admin'], function () {  
  Route::resource('log','LogController');
  Route::get('logout','LogController@logout');
});