问题
I use laravel 5.4 and I want to redirect these three different types of users to different pages
Schema
Types
+-------+-------------+
| id | name |
+-------+-------------+
| 1 | Super Admin |
| 2 | Admin |
| 3 | Cashier |
+-------+-------------+
Users
+-------+---------+-------------+
| id | type_id | name |
+-------+---------+-------------+
| 1 | 1 | Super Admin |
| 2 | 2 | Admin |
| 3 | 3 | Cashier |
+-------+---------+-------------+
LoginController
public function redirectTo()
{
if (Auth::user()->type_id === 1) {
return '/superAdmin/home';
}
elseif (Auth::user()->type_id === 2) {
return '/admin/home';
}
elseif (Auth::user()->type_id === 3) {
return '/kasir/home';
}
}
HomeController
public function superAdmin()
{
return view('superAdmin.home');
}
public function admin()
{
return view('admin.home');
}
public function kasir()
{
return view('kasir.home');
}
Routes
Route::get('/', 'HomeController@kasir');
Route::get('/admin/home', 'HomeController@admin');
Route::get('/superAdmin/home', 'HomeController@superAdmin');
I followed the answer of my previous question
redirecting three different user types/roles to different pages
I tried to sign in using a user with type_id = 1, which means the user is a super admin, but it always redirects to Route::get('/','HomeController@cashier');
as well as users with other roles
and the problem is that each role can access the page belongs to another role, what should I do to fix it?
回答1:
You can do one thing which i had been use in my last project using laravel.
You have to write this code in home controller's index method so while load home-page it going to check user type id and redirect to appropriate page.
if(Auth::check()) {
if(Auth::user()->in_usertype_id == 1) {
return view('admin.dashboard');
}elseif(Auth::user()->in_usertype_id == 2) {
return view('admin.dashboard1');
else{
return view('admin.dashboard3');
}
}else{
return redirect('login')->with('error',
Lang::get('message.unauthorize-access'));
}
Hope this will helps you.
来源:https://stackoverflow.com/questions/47925466/laravel-fails-to-redirect-multiple-authentication-to-different-pages