laravel 如何根据他们的角色将目标用户重定向到不同的路由?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31012985/
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
How to redirect intended user to a different route based on their role?
提问by Tropicalista
I'd like to redirect my user to different route, based on their role. I have two secured area in my app, "admin" and "dashboard". I'd like to check if user is authenticated, then redirect to intended, but if user has role editor it should be redirected to dashboard, otherwise if he has role admin should be redirected to admin area.
我想根据他们的角色将我的用户重定向到不同的路线。我的应用程序中有两个安全区域,“管理员”和“仪表板”。我想检查用户是否已通过身份验证,然后重定向到预期,但如果用户具有角色编辑器,则应将其重定向到仪表板,否则,如果用户具有角色 admin,则应将其重定向到管理区域。
I'm using AuthenticatesAndRegistersUsers class in my login. I have this on my custom controller:
我在登录中使用 AuthenticatesAndRegistersUsers 类。我的自定义控制器上有这个:
/**
* The default redirecTo path.
*
*/
protected $redirectTo = '/dashboard';
So when a user is authenticated it will be redirected to dashboard, but I'd like to check if the intended url is on admin group route and if user has admin role it should be redirected to admin area.
因此,当用户通过身份验证时,它将被重定向到仪表板,但我想检查预期的 url 是否在管理员组路由上,如果用户具有管理员角色,则应将其重定向到管理区域。
I'm using this middleware to redirect to login:
我正在使用这个中间件重定向到登录:
public function handle($request, Closure $next)
{
if ($this->auth->guest())
{
if ($request->ajax())
{
return response('Unauthorized.', 401);
}
else
{
return redirect()->guest('auth/login');
}
}
return $next($request);
}
采纳答案by Stuart Wagner
You could overwrite the redirectPath
method used by the trait in your AuthController
to inject the logic you need. Something like this:
您可以覆盖redirectPath
特征使用的方法AuthController
来注入所需的逻辑。像这样的东西:
/**
* Get the post register / login redirect path.
*
* @return string
*/
public function redirectPath()
{
// Logic that determines where to send the user
if (\Auth::user()->type == 'admin') {
return '/admin';
}
return '/dashboard';
}
EDIT:
编辑:
Laravel uses the following declaration in the AuthenticatesAndRegistersUsers
trait to redirect the user after successful login:
Laravel 在AuthenticatesAndRegistersUsers
trait 中使用以下声明在成功登录后重定向用户:
return redirect()->intended($this->redirectPath());
This will try to redirect the user to the previously attempted URL.
这将尝试将用户重定向到先前尝试的 URL。
If you need to redirect users to the right place when they're already logged in, that would be best done by adding more logic to your authentication middleware.
如果您需要在用户已经登录时将其重定向到正确的位置,最好通过向身份验证中间件添加更多逻辑来完成。
回答by sumit
Another approach is to override
authenticated
method
另一种方法是覆盖
authenticated
方法
public function authenticated()
{
if(Auth::check()) {
if(\Auth::user()->hasRole('Super Admin')) {
return redirect('/admin-dashboard');
} else {
return redirect('/user-dashbaord');
}
}
}
回答by Larigyn
I use this one. You also need to modify middleware RedirectIfAuthenticatedso that it won't route home. Just to different user's dashboard.
我用这个。您还需要修改中间件RedirectIfAuthenticated以便它不会路由回家。只是到不同用户的仪表板。
public function authenticated()
{
if($request->user()->hasRole('admin'))
{
// return redirect()->intended(route('admin.index'));
return redirect()->route('admin.index');
}
if($request->user()->hasRole('super'))
{
return redirect()->route('super.index');
}
if($request->user()->hasRole('officer'))
{
return redirect()->route('officer.index');
}
}