middleware
Skill4 starsChanged 4 months agoMITView on GitHub
Add PSR-15 middleware to a Slim app — understand the onion execution order, run before and after logic around the handler, and short-circuit the request when needed. Use when the user wants to add PSR-15 middleware to a Slim app or a specific route or group; the user needs Slim middleware that runs logic before and after the handler around $handler->handle(); the user is reasoning about the execution order of stacked Slim middleware and the onion model. Keywords: slim, middleware, psr15, onion, before-after, pipeline, php74.
2 other projects use a version of this.
Adopted by
Most recently taken up by
- gabriellst/codmtheir own version, first seen Aug 26, 2026
- jkaninda/okapi-skillstheir own version, first seen Aug 16, 2026
Other versions2 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 2
- gabriellst/codm★ 4same name
- jkaninda/okapi-skills★ 3same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository119 other files it gives its agents.
- strategy-bitrix-d7-incrementalSkill
- strategy-explicit-over-magicSkill
- strategy-framework-conventionsSkill
- strategy-legacy-safe-editsSkill
- strategy-minimal-diffSkill
- strategy-no-untested-refactorSkill
- strategy-preserve-public-apiSkill
- strategy-readonly-immutabilitySkill
- strategy-security-firstSkill
- strategy-test-drivenSkill
- openapiSkill
- paginationSkill
- rest-designSkill
- versioningSkill
- cqrsSkill
- dependency-inversionSkill
- hexagonalSkill
- repository-patternSkill
- service-layerSkill
- bitrixSkill
- findings-reportSkill
- laravelSkill
- performanceSkill
- phpSkill
- project-analysisSkill
- refactoringSkill
- roi-prioritizationSkill
- securitySkill
- stack-detectionSkill
- cache-layersSkill
- component-engineSkill
- d7-eventsSkill
- d7-loaderSkill
- d7-orm-queriesSkill
- iblock-elementsSkill
- kernel-main-moduleSkill
- localizationSkill
- controllersSkill
- ormSkill
- validationSkill
- controllersSkill
- query-builderSkill
- routingSkill
- autoload-psr4Skill
- dependency-managementSkill
- scriptsSkill
- connection-poolingSkill
- mysql-indexingSkill
- redisSkill
- transactionsSkill
- ci-github-actionsSkill
- deployment-strategiesSkill
- docker-phpSkill
- env-configSkill
- associationsSkill
- entities-mappingSkill
- orm-basicsSkill
- entity-apiSkill
- module-developmentSkill
- routing-controllersSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
7 rules. About 0 are common, 3 are distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 63
Do not run long-running or background work inside middleware on the request path.
- Distinctive: line 64
Do not build middleware for logic that concerns only a single route and is not cross-cutting.
- Very specific: line 72
Create a class implementing MiddlewareInterface with a process() method.
- Very specific: line 73
Do request work before $handler->handle($request), then decorate the returned response after it before returning.
- Very specific: line 74
Add the middleware with $app->add(), group ->add(), or $route->add(), remembering LIFO order.
- Very specific: line 78
Put request inspection and short-circuits before $handler->handle(), and response decoration (headers, timing) after it on the returned …
- Distinctive: line 105
Not applicable — no I/O, auth, or untrusted input.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
<!-- generated: do not edit — source: knowledge/atoms/slim/middleware/ --><!-- schema: atomVersion=3 skillVersion=2 graphVersion=1 --><!-- compile: @php-skills/compiler v1.0.0 (deterministic build) -->Slim Middleware
When to use
- The user wants to add PSR-15 middleware to a Slim app or a specific route or group.
- The user needs Slim middleware that runs logic before and after the handler around $handler->handle().
- The user is reasoning about the execution order of stacked Slim middleware and the onion model.
When NOT to use
- Do not run long-running or background work inside middleware on the request path.
- Do not build middleware for logic that concerns only a single route and is not cross-cutting.
Core guidance
- Implement MiddlewareInterface with process() — Write middleware as a class implementing PSR-15
MiddlewareInterface::process(Request $request, RequestHandler $handler)and call$handler->handle($request)to continue the chain.
Quick recipe
- Create a class implementing
MiddlewareInterfacewith aprocess()method. (verify: The class satisfies the PSR-15 interface.) - Do request work before
$handler->handle($request), then decorate the returned response after it before returning. (verify: Both the request and response phases run as expected.) - Add the middleware with
$app->add(), group->add(), or$route->add(), remembering LIFO order. (verify: The middleware runs at the intended position in the onion.)
Best practices
- Put request inspection and short-circuits before
$handler->handle(), and response decoration (headers, timing) after it on the returned response - Remember Slim runs middleware LIFO: the last one added wraps the others, so add the outermost concern (e.g
- Attach middleware at the narrowest useful scope:
$app->add()for global, group->add()for a section, and$route->add()for one route - To reject a request (e.g
Anti-patterns
| Anti-pattern | Why it fails | Do instead |
| Swallowing exceptions in middleware | Swallowed exceptions hide failures and return misleading 200 responses, masking real errors. | Let exceptions reach the error middleware, or rethrow after logging so the failure is visible. |
| Relying on implicit middleware ordering | Implicit ordering is fragile; a reorder or new file silently changes when auth or parsing runs. | Register middleware in one place with the intended onion order documented and grouped. |
| Middleware that mutates state instead of the message | Global side effects are invisible, order-dependent, and untestable, breaking the composable pipeline. | Pass data forward as request attributes on the delegated request instead of via global state. |
See references/anti-patterns.md.
Common mistakes
- Forgetting to call $handler->handle() — Middleware that never calls
$handler->handle()silently stops the chain, so the route handler never runs. Fix: Call$handler->handle($request)and return its response unless you intend to short-circuit. - Expecting request changes to propagate without passing them on — Attributes added with
$request->withAttribute(...)do not reach the handler unless the new request is passed to$handler->handle(). Fix: Capture the new request and delegate with it:$request = $request->withAttribute(...); return $handler->handle($request). - Middleware runs in the opposite order expected — Middleware appears to run in reverse because Slim executes the stack LIFO. Fix: Add middleware in reverse of the desired run order, or reason explicitly with the onion model.
Version notes
- Slim 4 standardized on PSR-15 middleware (PHP >=7.4) — Slim 4 uses the PSR-15
MiddlewareInterfacesignature; the Slim 3 double-passfunction ($req, $res, $next)signature is no longer the primary contract.
Security
- Not applicable — no I/O, auth, or untrusted input.
Testing
- Activation and decision behavior are verified in
eval/scenarios/slim/middleware.yaml.
Additional resources
- references/anti-patterns.md
- references/core.md
- references/examples.md
- references/pitfalls.md
- references/recipes.md
- references/version-differences.md
Knowledge graph
Prerequisites: slim/routing
<!-- graph-fragment: graph/fragments/slim/middleware.yaml -->Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

