RouteNotFoundException: Route [login] not defined, raised when a guest hits a protected route
Symfony\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.
Fix it fast
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 login2Match what you see, then jump to its fix
- 1
The 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
A browser page behind the auth middleware fails, and route:list shows nothing named loginThe application has no route named loginFix - 3
route:list shows a login page, but its name is something like admin.login or auth.loginThe login route exists under another nameFix - 4
routes/web.php defines the login route, but route:list does not show itCached routes are older than the route you addedFix
- 1
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/user2Fix
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/userConfirmed by raw.githubusercontent.com
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 login2Fix
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/dashboardConfirmed by raw.githubusercontent.com
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 login2Fix
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/dashboardConfirmed by raw.githubusercontent.com
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 login2Fix
Clear the route cache, and rebuild it as part of the deployment.
php artisan route:clear
# on production, during the deployment
php artisan route:cache3Verify
route:list now shows the login route, and the exception is gone.
php artisan route:list | grep -i loginConfirmed by raw.githubusercontent.com
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
- Laravel: Authentication (redirecting unauthenticated users)
- Laravel: Routing (named routes, route cache)
- Laravel: Error handling (shouldRenderJsonWhen)
- Laravel framework source: exception handler (unauthenticated)
- Laravel framework source: UrlGenerator (the exception message)
- Laravel framework source: expectsJson and wantsJson
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
A Laravel API call answers with a 302 redirect to a web page
Location: http://localhost/login
Laravel decides between a redirect and JSON from the request.
2 causes, each with a fixBack end
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack 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 fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack 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 fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fix