skip to Main Content

I want to define middleware in my Laravel project, this is my web.php file, when I try to check the auth, I get an error Route [login] not defined. Please help me define middleware in Laravel 11 in the resource controller and custom routes. By the way, I read everything in the laravel11 release notes, but I can’t figure out how I can fix this. Should i create my custom middleware… Thanks… All Routes

Route::redirect('/', 'cars.index');

Route::middleware(['guest'])->group(function(){
    Route::get('/login', [UserController::class, 'showLoginForm'])
    ->name('user.showLoginForm');

    Route::post('/login', [UserController::class, 'login'])
    ->name('user.login');

});

Route::middleware(['auth', 'verified'])->group(function() {
    Route::resources([
        'cars' => CarController::class,
        'user' => UserController::class
    ]);
    Route::post('/logout', [UserController::class, 'logout'])->name('user.logout');
});

I tried everything, I read everything about Laravel 11 release notes, and i tried to create a static middleware method inside my controller, but everything failed. I don’t really know how I can fix this… I am a beginner

2

Answers


  1. Chosen as BEST ANSWER

    I FIXED IT! The problem was my name on the login route, my name was user.showLoginForm, when I changed that to just login everything worked. Obviously Laravel auth is searching for the route named login and that is the problem. The middleware is now working. Thanks, everyone! All routes

    Route::get('/login', [UserController::class, 'showLoginForm'])
        ->name('user.showLoginForm');
    
    FIX!
    
    Route::get('/login', [UserController::class, 'login'])
        ->name('login');
    
    
    

  2. login is a default route name that comes from Auth::routes(). Occasionally, you will find your application still redirecting to these routes.

    My first idea would be to rename the route user.showLoginForm to login. You can also create a redirect from the login route to the user.showLoginForm route.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search