Middleware Laravel 8 Laravel 9 Laravel 10 Laravel 11 Laravel 12

Middleware Not Found Error in Laravel - Fix

This error occurs when Laravel cannot find a middleware class that's being referenced in routes or kernel.

The Error

Error Message
Target class [middleware] does not exist

Common Causes

  1. 1 Middleware not registered in Kernel
  2. 2 Typo in middleware alias
  3. 3 Middleware class file missing
  4. 4 Wrong namespace in middleware class
  5. 5 Using middleware alias that doesn't exist

Solutions

1

Register middleware in Kernel.php

PHP
// app/Http/Kernel.php
protected $middlewareAliases = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'admin' => \App\Http\Middleware\AdminMiddleware::class,
    'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];
2

Create middleware if it doesn't exist

Bash
php artisan make:middleware AdminMiddleware

# Then implement the handle method
public function handle($request, Closure $next)
{
    if (!auth()->user()?->is_admin) {
        abort(403);
    }
    return $next($request);
}
3

Use full class path if not registered

PHP
Route::middleware([\App\Http\Middleware\AdminMiddleware::class])
    ->group(function () {
        Route::get('/admin', [AdminController::class, 'index']);
    });
4

Laravel 11+ middleware registration

PHP
// In bootstrap/app.php
return Application::configure(basePath: dirname(__DIR__))
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->alias([
            'admin' => \App\Http\Middleware\AdminMiddleware::class,
        ]);
    })

Need Help With Your Laravel Project?

I specialize in building custom Laravel applications, process automation, and SaaS development. Whether you need to eliminate repetitive tasks or build something from scratch, let's discuss your project.

Currently available for 2-3 new projects

Hafiz Riaz

About Hafiz

Full Stack Developer from Italy. I build web applications with Laravel and Vue.js, and automate business processes. Creator of ReplyGenius, StudyLab, and other SaaS products.

View Portfolio

Related Errors