laravel-无法将多个身份验证重定向到不同的页面 [英] laravel - fails to redirect multiple authentication to different pages

查看:77
本文介绍了laravel-无法将多个身份验证重定向到不同的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用laravel 5.4,我想将这三种不同类型的用户重定向到不同的页面

I use laravel 5.4 and I want to redirect these three different types of users to different pages

架构

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');
}

路线

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

我尝试使用type_id = 1的用户登录,这意味着该用户是超级管理员,但它总是重定向到Route::get('/','HomeController@cashier');以及具有其他角色的用户

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?

推荐答案

您可以使用laravel完成上一个项目中使用的一件事.

You can do one thing which i had been use in my last project using laravel.

您必须使用家庭控制器的index方法编写此代码,因此在加载主页时,它将检查用户类型ID并重定向到适当的页面.

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.

这篇关于laravel-无法将多个身份验证重定向到不同的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆