Laravel Middleware Pattern Skill
Use this skill when implementing request filtering or modification logic.
Rules
- Modern Registration (Laravel 12)
-
Do NOT look for app/Http/Kernel.php (it is gone).
-
Register middleware in bootstrap/app.php using the ->withMiddleware() callback.
-
Use append() for global or alias() for route-specific.
->withMiddleware(function (Middleware $middleware) { $middleware->append(EnsureTokenIsValid::class); $middleware->alias([ 'admin' => EnsureUserIsAdmin::class, ]); })
- Structure
- Implement the handle method with proper typing.
public function handle(Request $request, Closure $next): Response { if (...) { return redirect('/home'); } return $next($request); }