Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

RouteNotFoundException: Route [login] not defined, raised when a guest hits a protected route

ErrorSymfony\Component\Routing\Exception\RouteNotFoundException: Route [login] not defined.

Short answer

When the auth middleware rejects a guest, Laravel either answers 401 JSON or redirects to the route named login. The error means it tried the redirect and no route has that name. Send Accept: application/json from API clients, name your login route login, or tell Laravel where guests go with redirectGuestsTo.

  • Applies to Laravel 12, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out whether the failing request came from a browser page or from an API client, and whether any route is called login. The route list shows the names that exist.

    php artisan route:list | grep -i login
  2. 2Match what you see, then jump to its fix

    1. 1The failing URL is an API route behind auth middleware, called from Postman, curl or a script, and the app has no web login pageAn API or AJAX request does not ask for JSONFix
    2. 2A browser page behind the auth middleware fails, and route:list shows nothing named loginThe application has no route named loginFix
    3. 3route:list shows a login page, but its name is something like admin.login or auth.loginThe login route exists under another nameFix
    4. 4routes/web.php defines the login route, but route:list does not show itCached routes are older than the route you addedFix

What does the error look like?

  • Symfony\Component\Routing\Exception\RouteNotFoundException: Route [login] not defined.
  • Route [login] not defined.

Cause 1 of 4

An API or AJAX request does not ask for JSON

1Check

For an unauthenticated request, Laravel's exception handler returns a 401 JSON response if the request expects JSON, and otherwise redirects the guest to route('login'). A request counts as expecting JSON when its first acceptable content type contains /json, which an Accept: application/json header provides.

curl -i https://example.com/api/user
curl -i -H "Accept: application/json" https://example.com/api/user

2Fix

Make the client send Accept: application/json. To return JSON for every route under a path, whatever the client sends, say so in bootstrap/app.php.

// bootstrap/app.php
use Illuminate\Http\Request;

->withExceptions(function (Exceptions $exceptions): void {
    $exceptions->shouldRenderJsonWhen(function (Request $request, Throwable $e) {
        if ($request->is('api/*')) {
            return true;
        }

        return $request->expectsJson();
    });
})

3Verify

The request without a token now returns 401 and a JSON message instead of the exception.

curl -i -H "Accept: application/json" https://example.com/api/user

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 4

The application has no route named login

1Check

The auth middleware redirects guests to the route named login unless told otherwise. If you set up authentication by hand, or removed a starter kit, no route has that name, and generating its URL fails.

php artisan route:list | grep -i login

2Fix

Name the login route login. Or, if guests belong on a path that has no name, set the destination in bootstrap/app.php.

// routes/web.php
Route::get('/login', [LoginController::class, 'show'])->name('login');

// or: send guests to a fixed path instead
// bootstrap/app.php
->withMiddleware(function (Middleware $middleware): void {
    $middleware->redirectGuestsTo('/signin');
})

3Verify

A guest request to the protected page redirects to the login page, with no exception.

curl -i https://example.com/dashboard

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 4

The login route exists under another name

1Check

route() looks up routes by name only. A login page named differently is invisible to the default redirect.

php artisan route:list | grep -i login

2Fix

Rename the route to login, since route names have to be unique, or tell the middleware which name to use.

// bootstrap/app.php
use Illuminate\Http\Request;

->withMiddleware(function (Middleware $middleware): void {
    $middleware->redirectGuestsTo(fn (Request $request) => route('admin.login'));
})

3Verify

A guest request to the protected page redirects to that login page.

curl -i https://example.com/admin/dashboard

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 4 of 4

Cached routes are older than the route you added

1Check

route:cache stores all routes in one file for production. The Laravel documentation warns that after you add new routes you need to generate a fresh route cache. Until you do, the new route is not known.

grep -n "name('login')" routes/*.php
php artisan route:list | grep -i login

2Fix

Clear the route cache, and rebuild it as part of the deployment.

php artisan route:clear
# on production, during the deployment
php artisan route:cache

3Verify

route:list now shows the login route, and the exception is gone.

php artisan route:list | grep -i login

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Make API clients send Accept: application/json, and test unauthenticated calls to every protected API route once.
  • In an API-only application, set shouldRenderJsonWhen for the api/* paths so a missing header never causes a redirect.
  • Keep the login route named login, or write the one place where you change the guest destination.
  • Run route:cache only in the deployment step, and route:clear whenever you add a route and see no effect.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

composer require maatwebsite/excel fails to install

Composer says the package needs an older PHP than the one installed

Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.

3 causes, each with a fix

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.