How to Fix the "Route [login] not defined" Error in Laravel

Have you ever run a Laravel application and suddenly gotten an error like this in the browser? Symfony\Component\Routing\Exception\RouteNotFoundException Route [login] not defined...

How to Fix the "Route [login] not defined" Error in Laravel

Have you ever run a Laravel application and suddenly gotten an error like this in the browser?

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

This error is very common among Laravel developers, especially for those just starting to learn or who have just enabled the auth middleware. This article will explain the cause in detail and provide several practical solutions.

The Cause of the Route [login] not defined Error

This error appears because Laravel's built-in auth middleware tries to redirect to a route named login when the user isn't authenticated. If a route named login isn't registered in your application, Laravel throws this exception.

Some common scenarios that cause it:

  • You applied the auth middleware to a route but haven't built an authentication (login) system.
  • You forgot to install or register an authentication package like Laravel Breeze or Jetstream.
  • A login route exists but isn't named login.
  • The routes/auth.php file isn't included in routes/web.php.

Solution 1: Install Laravel Breeze (Recommended)

If you really need full authentication features (login, register, forgot password), the easiest way is to use Laravel Breeze.

  1. Install the Breeze package via Composer:
composer require laravel/breeze --dev
  1. Run the Breeze install command:
php artisan breeze:install blade
  1. Install frontend dependencies and compile assets:
npm install && npm run dev
  1. Run the database migrations:
php artisan migrate

After that, a route named login will be automatically registered and the error will disappear.

Solution 2: Define the Login Route Manually

If you don't want to use Breeze and already have your own login controller, make sure you define a route named login in the routes/web.php file:

use App\Http\Controllers\Auth\LoginController;

Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('/login', [LoginController::class, 'login'])->name('login.post');
Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

Note the use of ->name('login') — this is what lets Laravel find the route when the auth middleware redirects.

Solution 3: Change the Redirect in the Auth Middleware

If you use a different route name for the login page, you can change the auth middleware's redirect configuration. Open the app/Http/Middleware/Authenticate.php file:

<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Http\Request;

class Authenticate extends Middleware
{
    protected function redirectTo(Request $request): ?string
    {
        return $request->expectsJson() ? null : route('auth.login'); // replace with your route name
    }
}

In Laravel 11, this configuration can also be set through bootstrap/app.php:

<?php

use Illuminate\Foundation\Application;

return Application::configure(basePath: dirname(__DIR__))
    ->withMiddleware(function ($middleware) {
        $middleware->redirectGuestsTo(fn () => route('auth.login'));
    })
    ->create();

Solution 4: Remove the Auth Middleware If It's Not Needed

If you accidentally applied the auth middleware to a route that should be publicly accessible, just remove that middleware from the relevant route.

Before (causing the error):

Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth'])->name('dashboard');

After (if the page is indeed public):

Route::get('/dashboard', function () {
    return view('dashboard');
})->name('dashboard');

How to Check the List of Registered Routes

To make sure the login route is registered, run the following Artisan command in the terminal:

php artisan route:list --name=login

If a line with the name login appears, the route is registered correctly. If the result is empty, you need to define it first using one of the solutions above.

Conclusion

The Route [login] not defined error in Laravel happens because the auth middleware can't find a route named login to redirect to. The best solution depends on your needs: use Laravel Breeze for full authentication instantly, or define the login route manually if you build your own auth system. Always make sure to use ->name('login') on the login page route so the middleware can find the redirect target correctly.

route login not defined laravel error route laravel laravel auth route laravel middleware auth fix error laravel route
Read Also

Here are some related articles that might help expand your knowledge:

Share this article
Back to Blog
🚀 Partner Recommendation

Need Premium Source Code & Business Apps?

Access Laravel applications, POS systems, School Management, Clinic Software, ERP solutions, and ready-to-use premium source code at GudangCode.

GudangCode
  • ✔ Premium Source Code
  • ✔ Ready-to-Use Systems
  • ✔ Lifetime Updates
  • ✔ Lifetime Membership
  • ✔ Daily App Updates
Join Membership →