How to change default redirect URL of Laravel 5 Auth filter?

后端 未结 10 1875
终归单人心
终归单人心 2020-12-04 20:17

By default if I am not logged and I try visit this in browser:

http://localhost:8000/home

It redirect me to http://localhost:8000/aut

相关标签:
10条回答
  • 2020-12-04 20:37

    For Laravel 5.4 You can set protected $redirectTo = '/'; in LoginController.php FILE. Or in RegistersUsers.php file you can

    protected function registered(Request $request, $user)
    {
        return redirect('tosomeRoute'); 
        //Note: This code will run when
        //The user has been registered
    }
    
    0 讨论(0)
  • 2020-12-04 20:40

    could you please outputs php artisan route:list please

    You are right you can set the following attributes:

    protected $loginPath = 'xxx';
    
    protected $redirectPath = 'xxx';
    
    protected $redirectAfterLogout = 'xxx';
    

    Set this attribute to you AuthController.php

    0 讨论(0)
  • 2020-12-04 20:42

    To change the redirection after the login, you only have to go to app/Http/Controllers/Auth/LoginController.php and add that inside the class LoginController:

    protected $redirectTo = '/redirect-url-here';
    

    Same for redirection after a new users register, but in that case, on AuthController.php

    0 讨论(0)
  • 2020-12-04 20:45

    In Laravel 5.6, go to app/Exceptions folder and open the Handler.php, add a new method that overrides the unauthenticated method like so:

    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if($request->ajax())
        {
            return response([
                "message" => "Unauthenticated.",
                "data" => [],
            ],401);
        }
    
        return redirect()->to('/');
    }
    

    This method is triggered when you access a protected route using the built-in "auth" middleware. Now you will have full control where to redirect or the response sent.

    0 讨论(0)
提交回复
热议问题