My Laravel Middleware Wasn't Running. The Registration Was in the Wrong File.
I wrote a Laravel middleware, attached it to a route, and nothing happened. No error, no redirect, no 403 — the request sailed straight past my check into the controller as if the middleware did not exist. The class was
I wrote a Laravel middleware, attached it to a route, and nothing happened. No error, no redirect, no 403 — the request sailed straight past my check into the controller as if the middleware did not exist.
The class was fine. The route was fine. The registration was the problem: I had put it in app/Http/Kernel.php, following an old tutorial. Laravel 11 removed that file, and Laravel 13 ignores it completely. Middleware is registered in bootstrap/app.php now, inside withMiddleware(). I moved one block of code there and the middleware started working instantly.
That is the single most common middleware mistake I see, and it is silent — no error tells you the kernel file is being ignored. If your middleware never runs, check the registration first.
The second most common mistake is order. My role check read $request->user()->role, but the middleware ran before auth, so the user was null and every visitor got a 403 — even logged-in editors. Listing auth first fixed it. Middleware runs in the order you list it, and that order is part of the logic.
The third is forgetting return $next($request). If handle() ends without returning anything, Laravel has no response to send and the page comes back blank. Every path through handle() must end in a return $next($request) or a returned response.
The full step-by-step — creating the class with Artisan, the role-check and logging examples, global vs group vs alias registration, attaching to routes and controllers, parameters, and the HasMiddleware interface detail that makes controller middleware actually run — is in my tutorial here: https://rajadevsolutions.online/blog/how-to-create-custom-middleware-in-laravel-13
One more tip: run php artisan route:list -v when something feels off. It shows every route with its middleware attached, so you can see in seconds whether yours is really on the route you think it is.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.