doupeng8494 2016-08-24 17:38
浏览 32
已采纳

Laravel:以不同方式重定向不同的用户角色

Is it possible to redirect users with different user roles to a different page in laravel 5.1?

I have looked into Auth middleware and Auth controller, but found nothing that processes the login request itself.

I have found something about login redirection here Laravel redirect back to original destination after login but i'm not sure where to put the suggested code snippets.

Can someone help me out

  • 写回答

1条回答 默认 最新

  • dongshadu2546 2016-08-24 18:05
    关注

    You can update in RedirectIfAuthenticated Middleware

    namespace App\Http\Middleware;
    
    use Closure;
    use Illuminate\Contracts\Auth\Guard;
    
    class RedirectIfAuthenticated
    {
        /**
         * The Guard implementation.
         *
         * @var Guard
         */
        protected $auth;
    
        /**
         * Create a new filter instance.
         *
         * @param  Guard  $auth
         * @return void
         */
        public function __construct(Guard $auth)
        {
            $this->auth = $auth;
        }
    
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @return mixed
         */
        public function handle($request, Closure $next)
        {
            if ($this->auth->check()) {
    
                $auth = Auth::user()->roles()->first();
    
                switch ($auth->role) {
                    case 'admin':
                            return  redirect()->route('admin');    
                        break;
                    case 'superadmin':
                            return  redirect()->route('superadmin'); 
                        break;
                    case 'user':
                            return  redirect()->route('user');  
                        break;
    
                    default:
                        # code...
                        return  redirect()->route('user');  
                        break;
                }   
    
             }
    
            return $next($request);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?