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

league/container

PSR-11–compliant dependency injection container from The PHP League. Register services, factories and shared instances, then resolve dependencies with autowiring support. Modern PHP (8.3+) with full docs, tests, and MIT license.

View on GitHub
Deep Wiki
Context7

league/container is a lightweight dependency injection container from The PHP League, built for modern PHP and aligned with PSR-11 for interoperable service resolution. It offers a clean, standards-focused foundation for wiring applications and managing dependencies without locking you into a framework.

  • PSR-11 compliant container interface support
  • Modern PHP support (PHP 8.3+) and PSR-4 autoloading
  • Documentation site with guides and examples
  • Tested with Pest and analyzed with PHPStan (Level 8)
  • MIT licensed and actively maintained
Frequently asked questions about Container
Can I use League\Container as a drop-in replacement for Laravel’s built-in container?
Yes, but with caveats. League\Container is PSR-11 compliant, so it works where Laravel’s container does, but you’ll need to adapt Laravel-specific features like `tag()`, `when()`, or `macro()`. Replace `app()` calls with `League\Container\Container::get()` and update service providers to use `add()` or `addShared()` instead of `bind()`.
How do I migrate Laravel’s service providers to work with League\Container?
Update your `register()` methods in service providers to use League’s container methods: `add()` for transient bindings and `addShared()` for singletons. For example, replace `$container->bind()` with `$container->add()`. Laravel’s `AppServiceProvider` will work if you adjust the binding syntax accordingly.
Will League\Container break Laravel’s Eloquent, middleware, or event systems?
It depends on how you configure it. Eloquent relies on container bindings, so manually register them (e.g., `add()` for `Illuminate\Database\Eloquent\Factory`). Middleware and events will work if their dependencies are properly bound, but Laravel’s container-specific features (like `tag()` for middleware groups) may require custom logic.
Is League\Container faster than Laravel’s container? When should I use it?
Yes, League\Container is optimized for speed and simplicity, making it ideal for performance-critical apps, CLI tools, or microservices where Laravel’s container feels overkill. Use it if you need a lightweight DI solution but still want PSR-11 compatibility with Laravel.
How do I handle Laravel’s `app()->when()` conditional bindings with League\Container?
League\Container doesn’t support `when()` natively. You’ll need to create a custom inflector or resolver to replicate conditional logic. Alternatively, use argument resolvers or delegate to a fallback container that handles complex scenarios.
Can I use League\Container alongside Laravel’s container for specific parts of my app?
Yes, you can instantiate League\Container separately and use it for non-critical dependencies (e.g., CLI commands or background jobs). For full integration, replace Laravel’s container via service provider bootstrapping, but test thoroughly to avoid conflicts.
Does League\Container support Laravel’s facades (e.g., `Auth`, `Cache`) out of the box?
No, facades like `Auth` or `Cache` are tightly coupled to Laravel’s container. You’ll need to mock or rewrite them to work with League\Container, or use a facade wrapper library like `laravel-facades` with custom bindings.
What PHP and Laravel versions does League\Container support?
League\Container supports PHP 8.3–8.5 and is PSR-11 compliant, so it works with any Laravel version that adheres to PSR-11 (Laravel 8+). However, Laravel-specific features (e.g., `macro()`) may require additional workarounds.
How do I test Laravel apps using League\Container for dependency injection?
League\Container simplifies testing by allowing easy mocking of services. Replace `app()` with `League\Container\Container::get()` in tests, and use `add()` to inject mocks. For example, bind a fake service in tests: `$container->add('service', fn() => new FakeService()).
Are there alternatives to League\Container for Laravel DI that offer more Laravel-specific features?
If you need Laravel-specific features like `tag()`, `when()`, or `macro()`, consider sticking with Laravel’s container or using a wrapper like `spatie/laravel-container`. For pure PSR-11 compliance with minimal overhead, League\Container is a strong choice, but expect to handle Laravel extensions manually.
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