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

lat4732's avatar
Level 12

Laravel 11 - change default login route

In Laravel 11, the Auth middleware is located in the vendor folder. I'm utilizing it for my authentication routes:

Route::prefix('account')->group(function () {

    Route::middleware(['auth'])->group(function () {
        Route::get('/', [AccountController::class, 'dashboard'])->name('account.dashboard');
    });

    Route::middleware(['guest'])->group(function () {
        Route::get('/login', [AccountController::class, 'login'])->name('account.login');
        Route::post('/authenticate', [AccountController::class, 'authenticate'])->name('account.authenticate');
        Route::get('/register', [AccountController::class, 'register'])->name('account.register');
        Route::post('/signup', [AccountController::class, 'signup'])->name('account.signup');
    });

});

When I try to access an auth route, such as account.dashboard, without being logged in, I want to be redirected to account.login instead of the default login route. How can I modify the Auth middleware logic to achieve this? Additionally, how can I make the same adjustment for the guest middleware?

0 likes
7 replies
lat4732's avatar
Level 12

@tykus Like this?

ha

The strange thing is, I can see that the file bootstrap/app.php has no impact on the application. Even if I introduce syntax errors in it, the application continues to function normally. I can navigate through different routes and perform actions without any issues. Maybe its some cache?

martinbean's avatar

@lat4732 That’s not what the bootstrap/app.php file looks like in Laravel 11.x. Is this an older application that you’ve upgraded?

2 likes
tykus's avatar

@lat4732 echoing @martinbean here... that is absolutely not the bootstrap/app.php file that is found in a new Laravel 11 application. It should look like this:

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        // here is where you can modify the redirect route...
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

You probably are looking at the wrong project in your editor/IDE

2 likes
lat4732's avatar
Level 12

My apologies, I was looking at the bootstrap/app.php file from another Laravel project. This is the correct way to proceed:

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Request;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->redirectGuestsTo(fn (Request $request) => route('account.login'));
        $middleware->redirectUsersTo(fn (Request $request) => route('account.dashboard'));
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();
1 like

Please or to participate in this conversation.