laravel-middleware-patterns
This skill teaches Laravel middleware architecture through practical patterns for request handling. Learn to build before and after middleware, implement terminable patterns for post-response tasks, organize middleware into groups, pass parameters to middleware, and apply common patterns like rate limiting, locale handling, and tenant scoping.
Laravel Middleware Patterns teaches you to architect request filtering and response modification using before, after, and terminable middleware patterns.
AI-generated summary based on this skill's SKILL.md
Decision gist · record as of 2026-04-19
Laravel Middleware Patterns teaches you to architect request filtering and response modification using before, after, and terminable middleware patterns. This skill teaches Laravel middleware architecture through practical patterns for request handling. Learn to build before and after middleware, implement terminable patterns for post-response tasks, organize middleware into groups, pass parameters to middleware, and apply common patterns like rate limiting, locale handling, and tenant scoping.
Use it when
- laravel-middleware-patterns covers creating middleware via `php artisan make:middleware` command.
- laravel-middleware-patterns explains before middleware executes before the request reaches your controller—ideal for validation.
Verify before relying
Read SKILL.md below before installing (1 file). Open directory: indexed for reading, not audited.
Install
iSerter/laravel-claude-agents/laravel-middleware-patterns · repository language: PHP
Open directory. Skills are indexed for reading, not audited. Review a skill's body before installing it.
Frequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
What are Laravel middleware best practices?
laravel-middleware-patterns teaches that middleware should follow single responsibility—each middleware handles one concern like authentication, CORS, or rate limiting. Use before/after patterns for request/response handling, implement terminable middleware for cleanup tasks after response sending, and organize related middleware into groups for cleaner route registration and maintainability.
How do you create middleware in Laravel?
laravel-middleware-patterns covers creating middleware via `php artisan make:middleware` command. Middleware receives a `$request` and `$next` callback; before middleware processes before passing to next layer, after middleware processes the response. Register middleware in `bootstrap/app.php` using `middleware()` method, assign aliases for routes, and add to groups for batch application across route collections.
What are before/after middleware patterns in Laravel?
laravel-middleware-patterns explains before middleware executes before the request reaches your controller—ideal for validation, authentication checks, or modifying requests. After middleware executes after the controller returns—useful for modifying responses, adding headers, or logging. Both patterns use the same structure; the difference is where you place logic relative to the `return $next($request)` call.
How does Laravel terminable middleware work with examples?
laravel-middleware-patterns describes terminable middleware as implementing a `terminate()` method that runs after the response is sent to the client. Use this for cleanup tasks like flushing logs, closing database connections, or recording metrics without delaying the user's response. Register terminable middleware normally; Laravel automatically calls `terminate()` after the response completes.
How do you configure middleware groups and aliases?
laravel-middleware-patterns shows middleware groups and aliases configured in `bootstrap/app.php`. Create aliases for individual middleware like `auth`, `cors`, or `throttle`, then group related aliases together—`web` group might include session and CSRF middleware, `api` group includes rate limiting. Apply groups to routes via `middleware()` method for batch registration.
What are common patterns like rate limiting and CORS in Laravel?
laravel-middleware-patterns covers rate limiting via `throttle` middleware with parameters like `throttle:60,1` (60 requests per minute), and CORS middleware for handling cross-origin requests with headers. Tenant scoping middleware isolates data per tenant, locale middleware sets application language, and security headers middleware adds protections like X-Frame-Options. Each pattern solves specific request/response concerns.
SKILL.md
Rendered from the published skill. Quoted content, verbatim.
Middleware Patterns
Creating Middleware
php artisan make:middleware EnsureUserIsSubscribed
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class EnsureUserIsSubscribed
{
public function handle(Request $request, Closure $next): Response
{
if (! $request->user()?->subscribed()) {
return redirect('billing');
}
return $next($request);
}
}
Before vs After vs Terminable Patterns
Before Middleware
// ✅ Runs before the request hits the controller
class CheckAge
{
public function handle(Request $request, Closure $next): Response
{
if ($request->age < 18) {
abort(403, 'Underage access denied.');
}
return $next($request);
}
}
After Middleware
```php // ✅ Runs after the response is generated class AddSecurityHeaders { public function handle(Request
(truncated - see the full file via the links below)
File tree — 1 file
skills/laravel-middleware-patterns/SKILL.md
Let your AI agent find skills like this
Example. Real query, live index.
You found this page by searching. An agent finds it by wishing: SkillFed indexes 56,283 agent skills by what they can do, searchable in plain language.
wish › “Learn Laravel middleware architecture and execution patterns”
Give your agent the search over MCP, or paste the wish link into any chat. No install? Search from any chat →
Related skills
This skill guides you through hardening Laravel applications against common vulnerabilities. Learn to configure Sanctum and Passport for secure authentication, implement role-based access control with gates and policies, enforce HTTPS, manage sessions safely, and validate passwords against compromised databases.
Master Laravel's built-in internationalization system to support multiple languages from the ground up. Learn to configure locales, structure translation files, use helpers in controllers and templates, and implement advanced features like translatable model attributes and route localization.
PHP Expert provides in-depth instruction on PHP 8+ language features including union types, named arguments, match expressions, attributes, and enums. Learn Laravel framework patterns, Composer dependency management, and object-oriented design principles for building robust applications.
Learn to implement authorization in Laravel using Gates for general ability checks and Policies for model-specific access rules. This skill covers middleware integration, Blade directives for conditional rendering, and Response objects for detailed permission messages. Includes testing strategies and best practices for securing controllers and form requests.
Learn how to structure Laravel API Resources for clean model transformation and consistent JSON responses. This skill covers conditional attributes, relationship loading, collections with pagination, and techniques to prevent N+1 queries.
Get expert guidance on Laravel 12.x application development, from foundational concepts like routing and database migrations to advanced patterns including Eloquent relationships, API design, and authentication with Sanctum. This skill covers the full development lifecycle—models, validation, queues, dependency injection, and best practices for building scalable web applications.
More skills laravel-specialist (MIT)