Laravel 5.4 redirection to custom url after login

前端 未结 9 2179
悲哀的现实
悲哀的现实 2020-11-28 09:46

I am using Laravel Framework 5.4.10, and I am using the regular authentication that

php artisan make:auth

provides. I want to protect the

相关标签:
9条回答
  • 2020-11-28 10:26

    If you look in the AuthenticatesUsers trait you will see that in the sendLoginResponse method that there is a call made to $this->redirectPath(). If you look at this method then you will discover that the redirectTo can either be a method or a variable.

    This is what I now have in my auth controller.

    public function redirectTo() {
        $user = Auth::user();
        switch(true) {
            case $user->isInstructor():
                return '/instructor';
            case $user->isAdmin():
            case $user->isSuperAdmin():
                return '/admin';
            default:
                return '/account';
        }
    }
    
    0 讨论(0)
  • 2020-11-28 10:29

    That's what i am currrently working, what a coincidence.

    You also need to add the following lines into your LoginController

    namespace App\Http\Controllers\Auth;
    
    use App\Http\Controllers\Controller;
    
    use Illuminate\Foundation\Auth\AuthenticatesUsers;
    
    use Illuminate\Http\Request;
    
    class LoginController extends Controller
    {
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */
    
    use AuthenticatesUsers;
    
    
    protected function authenticated(Request $request, $user)
    {
    if ( $user->isAdmin() ) {// do your magic here
        return redirect()->route('dashboard');
    }
    
     return redirect('/home');
    }
    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    //protected $redirectTo = '/admin';
    
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }
    }
    
    0 讨论(0)
  • 2020-11-28 10:29
    1. Go to Providers->RouteServiceProvider.php

    2. There change the route, given below:

      class RouteServiceProvider extends ServiceProvider
      {
       protected $namespace = 'App\Http\Controllers';
      
       /**
        * The path to the "home" route for your application.
        *
        * @var string
        */
       public const HOME = '/dashboard';
      
    0 讨论(0)
提交回复
热议问题