douge7771 2017-04-17 08:48
浏览 131
已采纳

登录后的登录路线上的自定义登录重定向 - laravel

I am Creating Student Portal For My university.

There are multiply users in This portal e.g student , teacher , staff , & administration.

I have Successfully Created Custom login redirect based on Multiply Users e.g when Student login he redirected on localhost:8000/student/dash-board similarly teacher redirected on localhost:8000/teacher/dash-board .. ..

LoginController.php (Works Perfectly After login)

   public function redirectTo()
{
    $userRollId = Auth::user()->user_role_id;

    if( $userRollId == 1)
    {
           return route('administrationDashboard');
    }
    elseif ($userRollId == 2) {

            return route('teacherDashboard');
    }
    elseif ($userRollId == 3) {
            return route('studentDashboard');
    }
    elseif ($userRollId == 4) {
            return route('departmentalStaffDashboard');
    }
     elseif ($userRollId == 4) {
            return 'departmentalStaff/dashboard';       
    }
}

But I am facing problem while I am testing my Application.

Senior is that I logged in as Student and after login redirect on http://localhost:8000/student/dash-board

For test when I hit localhost:8000/login Its redirect to localhost:8000/home It should redirect on localhost:8000/student/dash-board but not :(

I think LoginController logic is not apply on this after login. Maybe LoginController logic Only Apply when we try to login.

So there is any solution or Suggestion ??

  • 写回答

1条回答 默认 最新

  • dsio68964998 2017-04-17 09:54
    关注

    I assume that you are using LoginController of laravel. In LoginController, it attach the guest middleware to all the route except logout (Check Here).

    So you have to add the code to \App\Http\Middleware\RedirectIfAuthenticated class.

    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            $userRollId = Auth::user()->user_role_id;
    
            if( $userRollId == 1)
            {
                return route('administrationDashboard');
            }
            elseif ($userRollId == 2) {
    
                return route('teacherDashboard');
            }
            elseif ($userRollId == 3) {
                return route('studentDashboard');
            }
            elseif ($userRollId == 4) {
                return route('departmentalStaffDashboard');
            }
            elseif ($userRollId == 4) {
                return 'departmentalStaff/dashboard';
            }
        }
    
        return $next($request);
    }
    

    Better thing is to create a static function :

    Class Helper {
        public static function redirectByRole($userRollId) {
            if( $userRollId == 1)
                {
                    return route('administrationDashboard');
                }
                elseif ($userRollId == 2) {
    
                    return route('teacherDashboard');
                }
                elseif ($userRollId == 3) {
                    return route('studentDashboard');
                }
                elseif ($userRollId == 4) {
                    return route('departmentalStaffDashboard');
                }
                elseif ($userRollId == 4) {
                    return 'departmentalStaff/dashboard';
                }
            }
        }
    }
    

    And call it like this :

    From your controller :

    public function redirectTo()
    {
        $userRollId = Auth::user()->user_role_id;
    
        return Helper::redirectByRole($userRollId);
    }
    

    From RedirectIfAuthenticated middleware :

    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            $userRollId = Auth::user()->user_role_id;
    
            return Helper::redirectByRole($userRollId);
        }
    
        return $next($request);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog