我想基于他们的角色将我的用户重定向到不同的路线.我的应用程序中有两个安全区域,"admin"和"dashboard".我想检查用户是否经过身份验证,然后重定向到预期,但如果用户有角色编辑器,则应将其重定向到仪表板,否则如果他有角色,则应重定向到管理区域.
我在登录时使用了AuthenticatesAndRegistersUsers类.我在自定义控制器上有这个:
/**
* The default redirecTo path.
*
*/
protected $redirectTo = '/dashboard';
Run Code Online (Sandbox Code Playgroud)
因此,当用户通过身份验证时,它将被重定向到仪表板,但我想检查预期的URL是否在管理组路由上,如果用户具有管理员角色,则应将其重定向到管理区域.
我正在使用此中间件重定向到登录:
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);
}
Run Code Online (Sandbox Code Playgroud)
您可以覆盖您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';
}
Run Code Online (Sandbox Code Playgroud)
编辑:
Laravel在AuthenticatesAndRegistersUsers特征中使用以下声明在成功登录后重定向用户:
return redirect()->intended($this->redirectPath());
Run Code Online (Sandbox Code Playgroud)
这将尝试将用户重定向到先前尝试的URL.
如果您需要在用户登录时将用户重定向到正确的位置,那么最好通过向身份验证中间件添加更多逻辑来实现.
| 归档时间: |
|
| 查看次数: |
10028 次 |
| 最近记录: |