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 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题