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
Install
iSerter/laravel-claude-agents/laravel-middleware-patterns · repository language: PHP
git clone https://github.com/iSerter/laravel-claude-agents
cp -r laravel-claude-agents/skills/laravel-middleware-patterns ~/.claude/skills/laravel-middleware-patternsnpx skillfed install iSerter/laravel-claude-agents/laravel-middleware-patternsFrequently 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)
Read as markdown · JSON record · Browse the source repository
File tree — 1 file
skills/laravel-middleware-patterns/SKILL.md