Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

igaster's avatar
Level 11

Laravel 5.4 change login url

Hi!

When the auth middleware fails, the user is redirected to the \login url. How can we change this default url to something else?

The autentication routes seem to be hardcoded at Illuminate\Routing\Router::auth() function:

    public function auth()
    {
        // Authentication Routes...
        $this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
        $this->post('login', 'Auth\LoginController@login');
        $this->post('logout', 'Auth\LoginController@logout')->name('logout');

        // etc...
    }

I don't know when they are registered (I don't expicit register them in my App)

I tried to redifine them at routes/web.php but nothing happens...

0 likes
9 replies
KamalKhan's avatar

One way is to use a middleware:

  • Create \app\Http\Middleware\RedirectIfNotAuthenticated.php
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfNotAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (! Auth::guard($guard)->check()) {
            return redirect('/somewhere');
        }

        return $next($request);
    }
}
  • Use the middleware in your route
use App\Http\Middleware\RedirectIfNotAuthenticated;

Route::middleware(RedirectIfNotAuthenticated::class)->get(....);
2 likes
ricardoarg's avatar

they are registered when you have Auth::routes() in your web.php

you could delete that "Auth::routes()" line and put all the auth routes manually pointing wherever you want:

Route::get('`myNewLoginPageYay/', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login/', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');

// Registration Routes...
Route::get('register/', 'Auth\RegisterController@showRegistrationForm')->name('register');
Route::post('register/', 'Auth\RegisterController@register');

// Password Reset Routes...
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');

3 likes
igaster's avatar
igaster
OP
Best Answer
Level 11

After some investigation I found that when an AuthenticationException is thrown then the unauthenticated() function is triggered located in in app\Exceptions\Handler.php, which returns:

return redirect()->guest('/login');

so changing it to:

return redirect()->guest('/custom/login/url');

will do the job!

for named routes you can use:

return redirect()->guest(route('name', [], false));

Thanks everyone!

1 like
laics1984's avatar

I received the error message: Sorry, the page you are looking for could not be found. NotFoundHttpException in RouteCollection.php line 161:

Do i need to create a new view for this custom login url?

igaster's avatar
Level 11

@JohnnyL:

Alert! Files in 'vendor' folder will be replaced on next "composer update"

1 like
JohnnyL's avatar

Thanks @igaster, I'll edit my post. I thought it was weird that noone had found this simple fix :P

1 like
saidbakr's avatar

@igaster I have done the following as you described, but it leads to Error 404 :

return redirect()->guest('/custom/login/url');
jrizk's avatar

I'm doing the same task. Rerouting Guests to '/login' . I edited the below in UserController.php:

    public function __construct(UserApiController $API)
     {
           $this->UserAPI = $API;
    
           $this->middleware('auth', ['except' => ['login']]);
            //$this->middleware('auth', ['except' => ['index','about','single_video','all_categories' ,'category_videos' , 'sub_category_videos' ,  'about' , 'contact','trending']]);
}

and edited the below in Handler.php:

        public function render($request, Exception $e)
            {
                return redirect()->guest('/login');
                //return response()->view('errors.404', [], 404);
                // return parent::render($request, $e);
            }
        }

It redirect well but when user is signed in and tryiing to visit a protected blade, I'm receiving the below error in laravel.log:

[2020-02-24 16:10:51] production.ERROR: ReflectionException: Class App\Http\Controllers\UserController      does not exist in /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Route.php:281

What have I missed?

jrizk's avatar

actually noticed some closes where missing from the UserController.php file. fixed that. working fine now

Please or to participate in this conversation.