在我的应用中,我有两种类型的用户 - 管理员和教师。在我的AuthController中我有这个:
public function getLogin() {
return view('auth.login');
}
public function postLogin(\Illuminate\Http\Request $request) {
if(Auth::attempt([
'username' => $request->input('username'),
'password' => $request->input('password'),
'type' => 'admin'
])) {
return redirect ('admin');
}
if(Auth::attempt([
'username' => $request->input('username'),
'password' => $request->input('password'),
'type' => 'teacher'
])) {
return redirect ('educator/account');
}
return redirect('login')->with('message', [
'type' => 'danger',
'message' => 'Грешно потребителско име или парола!'
]);
}
public function getLogout() {
Auth::logout();
return redirect('login');
}
但是当我以类型教师用户身份登录时,如果我转到 http://localhost/school_system/public/admin ,我会自动转到管理面板而不会要求输入用户名和密码。我想如果我想从教师帐户进入管理面板,这就要求输入用户名和密码,我该怎么做呢?
我的路线:
Route::group(['middleware' => ['auth']
], function () {
Route::group([
'prefix' => 'admin',
'namespace' => 'Admin'
], function () {
Route::controller('student', 'StudentsController');
Route::controller('profile', 'ProfilesController');
Route::controller('class', 'ClassesController');
Route::controller('subjects', 'SubjectsController');
Route::controller('teacher', 'TeachersController');
Route::controller('marktype', 'MarkTypeController');
Route::controller('rules', 'RuleController');
Route::get('{slug?}', 'PageController@getView');
});
});
Route::group(['middleware' => ['auth']
], function () {
Route::group([
'prefix' => 'educator',
'namespace' => 'Educator'
], function () {
Route::controller('account', 'AccountController');
Route::get('{slug?}', 'EducatorController@getView');
});
});
谢谢^^
答案 0 :(得分:2)
您可以创建一个" adminmiddleware" :php artisan make:middleware AdminMiddleware
您可以使用的代码示例
if ($request->user()->type != 'admin')
{
return redirect('home');
}
并在路线中:
Route::group(['middleware' => ['admin']...
完成;)