agentleFS
AnyOrgNameAnyOrgName/php-skills/skills/slim/dependency-injection/SKILL.md

dependency-injection

Skill4 starsChanged 4 months agoMITView on GitHub

Wire a PSR-11 container into a Slim app — register service definitions, resolve invokable controllers from the container, and inject dependencies instead of using globals. Use when the user needs to register service definitions in the Slim container so classes get their dependencies; the user wants Slim to resolve an invokable controller class from the container instead of a closure; the user wants to configure a PSR-11 container for a Slim app via AppFactory::setContainer. Keywords: slim, dependency-injection, psr11, container, definitions, php74.

14 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use api-versioning, caching and 6 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • wpankSkill

    api-versioning

    API versioning strategies — URL path, header, query param, content negotiation — with breaking change classification, deprecation timelines, migration patterns, and multi-version support. Use when evolving APIs, planning breaking changes, or managing version lifecycles.

    Also in 3 projects that use this

  • wpankSkill

    caching

    Caching strategies, invalidation, eviction policies, HTTP caching, distributed caching, and anti-patterns. Use when designing cache layers, choosing eviction policies, debugging stale data, or optimizing read-heavy workloads.

    Also in 3 projects that use this

  • diego-tobalinaSkill

    logging

    Logging best practices for Java (SLF4J/Logback) and Node.js (Winston/Pino). Use for structured logging, log levels, and avoiding sensitive data exposure.

    Also in 4 projects that use this

  • Model Context ProtocolSkill

    project-structure

    Where things live in the Inspector repo — which client owns which surface, what is in core/ versus clients/web, and what each top-level directory is for. Use when locating code, deciding where a new module belongs, or answering which package owns or contains a given area of the codebase.

    Also in 3 projects that use this

  • harrissohail6Skill

    clean-architecture

    Clean architecture and SOLID principles. Auto-activates when designing systems or refactoring code structure.

    Also in 3 projects that use this

  • JohnJBorenSkill

    ci-cd

    CI/CD pipeline design, GitHub Actions, testing automation, and deployment strategies

    Also in 3 projects that use this

The file

We split this skill into the rules it gives an agent

5 rules. 0 are common, 4 are distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 13 edited versions we read keep these.

  • 4

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Do not reach for this skill to pull services out of the container inside handlers on demand.

    line 64
  2. Distinctive:

    Do not introduce a container for a trivial Slim app with a couple of routes and no shared services.

    line 65
  3. Very specific:

    Create a PSR-11 container (e.g.

    line 73
  4. Distinctive:

    Add definitions binding interfaces to implementations and factory closures for configured services.

    line 74
  5. Distinctive:

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

    line 106

“Common” is measured: kept by at least 6 in 10 of the 13 edited versions of this file we could read. “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/dependency-injection/ --><!-- schema: atomVersion=3 skillVersion=2 graphVersion=1 --><!-- compile: @php-skills/compiler v1.0.0 (deterministic build) -->

Slim Dependency Injection

When to use

  • The user needs to register service definitions in the Slim container so classes get their dependencies.
  • The user wants Slim to resolve an invokable controller class from the container instead of a closure.
  • The user wants to configure a PSR-11 container for a Slim app via AppFactory::setContainer.

When NOT to use

  • Do not reach for this skill to pull services out of the container inside handlers on demand; that is a service-locator anti-pattern, not injection.
  • Do not introduce a container for a trivial Slim app with a couple of routes and no shared services.

Core guidance

  • Set the container before creating the app — Build the PSR-11 container, pass it with AppFactory::setContainer($container) before AppFactory::create(), so Slim uses it to resolve handlers.

Quick recipe

  1. Create a PSR-11 container (e.g. PHP-DI), add definitions, and call AppFactory::setContainer($container) before AppFactory::create(). (verify: The app boots and resolves class-name handlers.)
  2. Add definitions binding interfaces to implementations and factory closures for configured services. (verify: Requesting an interface from the container returns the bound implementation.)
  3. Register the route with Controller::class and declare its dependencies as constructor parameters. (verify: The controller runs with its dependencies injected.)

Best practices

  • Let the container autowire classes whose dependencies are other typed classes, and write a factory definition (closure receiving the container) only when a service needs configuration or a chosen implementation
  • Declare each dependency as a constructor parameter and let the container supply it, rather than storing the container and calling $c->get(...) inside methods
  • Register a definition mapping each interface to a concrete class so controllers can type-hint the interface and receive the bound implementation
  • Register routes with a Controller::class (invokable, or [Controller::class, 'method']) so the container instantiates the controller with its dependencies

Anti-patterns

Anti-patternWhy it failsDo instead
Injecting the container into a controllerDepending on the container hides real dependencies and turns the class into an untestable service locator.Inject the specific services the controller needs as constructor parameters.
Constructing dependencies with new inside handlersHard-coding construction couples the handler to concrete classes and their own dependencies, defeating the container.Register the service in the container and inject it so construction stays in one place.
Reaching for global singletons instead of the containerGlobal access bypasses the container, hides dependencies, and makes handlers impossible to test in isolation.Register the service in the container and inject it into the handler or controller.

See references/anti-patterns.md.

Common mistakes

  1. Container ignored because it was set too late — Class-name route handlers fail to resolve because the container was set after AppFactory::create(). Fix: Call AppFactory::setContainer($container) before AppFactory::create().
  2. NotFoundException for an unregistered service — Resolving an interface or non-autowirable service throws a container NotFoundExceptionInterface. Fix: Add a definition mapping the interface or service id to a concrete factory or class.
  3. Shared service accumulates request-specific state — A container service is shared (built once), so storing per-request state on it leaks across requests in long-running servers. Fix: Keep shared services stateless; pass per-request data as method arguments or request attributes.

Version notes

  • Slim 4 requires an external PSR-11 container (PHP >=7.4) — Slim 4 ships without a built-in container and expects any PSR-11 container (PHP-DI, League Container) via AppFactory::setContainer; Slim 3 bundled Pimple with a different API.

Security

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

Testing

  • Activation and decision behavior are verified in eval/scenarios/slim/dependency-injection.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

Related: slim/routing

<!-- graph-fragment: graph/fragments/slim/dependency-injection.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.