agentleFS
AnyOrgNameAnyOrgName/php-skills/skills/slim/middleware/SKILL.md

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

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
  1. Distinctive:

    Do not run long-running or background work inside middleware on the request path.

    line 63
  2. Distinctive:

    Do not build middleware for logic that concerns only a single route and is not cross-cutting.

    line 64
  3. Very specific:

    Create a class implementing MiddlewareInterface with a process() method.

    line 72
  4. Very specific:

    Do request work before $handler->handle($request), then decorate the returned response after it before returning.

    line 73
  5. Very specific:

    Add the middleware with $app->add(), group ->add(), or $route->add(), remembering LIFO order.

    line 74
  6. Very specific:

    Put request inspection and short-circuits before $handler->handle(), and response decoration (headers, timing) after it on the returned …

    line 78
  7. Distinctive:

    Not applicable — no I/O, auth, or untrusted input.

    line 105

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md
<!-- 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

  1. Create a class implementing MiddlewareInterface with a process() method. (verify: The class satisfies the PSR-15 interface.)
  2. 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.)
  3. 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-patternWhy it failsDo instead
Swallowing exceptions in middlewareSwallowed 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 orderingImplicit 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 messageGlobal 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

  1. 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.
  2. 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).
  3. 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 MiddlewareInterface signature; the Slim 3 double-pass function ($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 -->

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.