How to change default redirect URL of Laravel 5 Au

2019-01-16 16:41发布

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/auth/login

How can I change to redirect me to http://localhost:8000/login

标签: php laravel-5
10条回答
\"骚年 ilove
2楼-- · 2019-01-16 17:37

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

查看更多
forever°为你锁心
3楼-- · 2019-01-16 17: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
}
查看更多
你好瞎i
4楼-- · 2019-01-16 17:43

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

查看更多
Lonely孤独者°
5楼-- · 2019-01-16 17:43

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.

查看更多
登录 后发表回答