Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Container Laravel Package

psr/container

The PSR-11 Container Interface defines core abstractions for dependency injection in PHP. It’s a standard abstraction, not an implementation—used by DI containers like Laravel’s Illuminate\Container. Essential for dependency management, ensuring interoperability across container implementations.

View on GitHub
Deep Wiki
Context7

Getting Started

The psr/container package provides only the interface definitions for PSR-11 — it’s not a dependency injection container itself. To get started:

  1. Require it: composer require psr/container (most modern DI containers already require it transitively).
  2. Implement or consume: If building a DI container, implement Psr\Container\ContainerInterface. If consuming one (e.g., Laravel’s resolver, Symfony’s Container, Symfony DI, PHP-DI), you don’t need to install this directly — it’s a dependency of your chosen container.
  3. First use case: Type-hint Psr\Container\ContainerInterface in your services or libraries to accept any PSR-11-compliant container, enabling decoupling and interoperability.

Implementation Patterns

  • Laravel integration: While Laravel’s service container isn’t directly PSR-11-compliant, you can wrap it using app()->make(ContainerInterface::class) (via illuminate/container’s adapter), or leverage Container::add('psr/container', ...).
  • Library design: Build your package against ContainerInterface instead of concrete containers (e.g., Symfony, PHP-DI) to maximize compatibility.
  • Testing: Mock ContainerInterface easily in unit tests (e.g., Mockery::mock(ContainerInterface::class)) without pulling in full container implementations.
  • Middleware pipelines: Integrate PSR-11 containers with PSR-15/PSR-13 middlewares — many HTTP frameworks (e.g., Slim, Nyholm/HTTP Server) expect PSR-11 for dependency injection in handlers.

Gotchas and Tips

  • PHP version requirement: psr/container 2.x requires PHP ≥ 7.4 (due to ContainerExceptionInterface extending Throwable). For older PHP, use psr/container 1.0–1.2 (but avoid — outdated and insecure).
  • No implementation included: This package only defines interfaces. Implementations (e.g., symfony/service-contracts, php-di/php-di, container-interop/container-interop) must be installed separately.
  • Type accuracy: PHPStan/Psalm may complain if you rely on outdated container docs — ensure you’re using psr/container ≥ 2.0 for has() to return bool and $id as string.
  • Exception handling: When catching ContainerExceptionInterface, remember it is a Throwable — always pair get() with try/catch, especially when resolving optional dependencies.
  • Avoid overuse: Don’t inject ContainerInterface everywhere — prefer explicit constructor injection for better testability. Reserve ContainerInterface for adapter layers, factories, or dynamic resolution scenarios (e.g., plugin systems).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport