douge7771
2017-04-17 08:48 阅读 99
已采纳

登录后的登录路线上的自定义登录重定向 - 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 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);
    }
    
    点赞 评论 复制链接分享

相关推荐