laravel 5 根据用户角色登录后重定向用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36812134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:39:34  来源:igfitidea点击:

laravel 5 redirect user after login based on user's role

laravellaravel-5

提问by Salar

My 'users' table has a 'role' column and when users are registered or logged in, I want them to be redirected based on their role column. how can I do that?

我的“用户”表有一个“角色”列,当用户注册或登录时,我希望根据他们的角色列重定向他们。我怎样才能做到这一点?

回答by Salar

I added this function to AuthController.php and everything fixed magically

我将此功能添加到 AuthController.php 并且一切都神奇地修复了

public function authenticated($request , $user){
    if($user->role=='super_admin'){
        return redirect()->route('admin.dashboard') ;
    }elseif($user->role=='brand_manager'){
        return redirect()->route('brands.dashboard') ;
    }
}

回答by SlateEntropy

If you are using the Authentication system provided with Laravel you can override the redirectPathmethod in your Auth\AuthController.

如果您正在使用提供Laravel认证系统可以覆盖redirectPath在你的方法Auth\AuthController

For example, this would redirect a user with role 'admin' to /adminand any other user to /account:

例如,这会将具有角色“admin”的用户重定向到/admin任何其他用户/account

public function redirectPath()
{
    if (\Auth::user()->role == 'admin') {
        return "/admin";
        // or return route('routename');
    }

    return "/account";
    // or return route('routename');
}

You could also use Laravel Authorization(introduced in 5.1.11) to manage role logic.

您还可以使用Laravel 授权(在 5.1.11 中引入)来管理角色逻辑。

回答by Mohamad Osama

In laravel 5.7 there is no AuthController.php so you have to go Controllers\Auth\LoginController.phpand add the below function,

在 Laravel 5.7 中没有 AuthController.php 所以你必须去Controllers\Auth\LoginController.php并添加以下函数,

If the redirect path needs custom generation logic you may define a redirectTomethod instead of a redirectToproperty

如果重定向路径需要自定义生成逻辑,您可以定义redirectTo方法而不是redirectTo属性

protected function redirectTo()
{

    if($user->role=='super_admin'){
        return '/path1';
    }elseif($user->role=='brand_manager'){
        return '/path2';
    }   

}

回答by MAik pf

You can do this handle the request in the Middleware RedirectIfAuthenticated.php inside the handle function like this:

您可以在 handle 函数内的 Middleware RedirectIfAuthenticated.php 中处理请求,如下所示:

/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @param  string|null  $guard
 * @return mixed
 */
public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        if (Auth::user()->role == 'admin') {
            return redirect('/admin');
        }else{
            return redirect('/');
        }
    }

    return $next($request);
}

回答by Evan

I write this in case someone consider useful. If you want to redirect always and not only on Login, I did it on web.php like this:

我写这个以防有人认为有用。如果您想始终重定向而不仅仅是在登录时重定向,我在 web.php 上这样做:

  Route::get('/home', function () {
    switch(\Illuminate\Support\Facades\Auth::user()->role){
        case 'admin':
            return redirect(route('home.admin'));
            break;
        case 'employee':
            return redirect(route('home.employee'));
            break;
        default:
            return '/login';
            break;
    }
});

Route::get('/home/admin', 'HomeController@indexAdmin')->name('home.admin');
Route::get('/home/employee', 'HomeController@indexEmployee')->name('home.employee');

So every time they click on /home is redirect to the url you want, you could do it too with '/'.

因此,每次他们单击 /home 时都会重定向到您想要的 url,您也可以使用“/”来实现。