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

league/container is a lightweight PSR-11 dependency injection container for PHP. Define entries, factories, and autowiring-friendly services to manage application dependencies cleanly, with modern PHP support and solid tooling for testing and analysis.

View on GitHub
Deep Wiki
Context7

Small but powerful dependency injection container

Frequently asked questions about Container
Can I use league/container as a drop-in replacement for Laravel’s built-in Service Container?
Yes, but with caveats. league/container is PSR-11 compliant, so it can replace Laravel’s container for non-framework dependencies. However, Laravel-specific features like autowiring, facades, or `app()->tagged()` require custom wrappers or hybrid integration. For full compatibility, delegate framework services to Laravel’s container while using league/container for third-party services.
How do I register a service with league/container in Laravel?
Use `add()` for non-shared instances or `addShared()` for singletons, mirroring Laravel’s `bind()` and `singleton()`. Example: `$container->addShared(MyService::class, fn() => new MyService($config));`. For factories, pass a closure. The API is simpler than Laravel’s but lacks dynamic binding methods like `when()` or `tag()`.
Does league/container support Laravel’s autowiring?
No, league/container does not include Laravel’s autowiring. You’ll need to manually register services or use reflection-based resolution (e.g., `Container::get()` with constructor arguments). For autowiring, consider sticking with Laravel’s container or using a wrapper like `league/container` with custom logic to mirror Laravel’s behavior.
Is league/container compatible with Laravel 10/11 and PHP 8.3+?
Yes, league/container supports PHP 8.3–8.5 and integrates with Laravel 10/11 via PSR-11. However, Laravel’s container has evolved (e.g., stricter typing in PHP 8.1+), so test thoroughly. Use `league/container` for greenfield projects or hybrid setups where you delegate non-framework services to it.
How do I mock league/container in Laravel tests?
Laravel’s `MockApplication` and `resolveClass()` helpers won’t work. Use league/container’s built-in mocking (e.g., `Container::get()` with fake implementations) or PHPUnit’s native mocking. For Pest, leverage its mocking features or create a test container instance with pre-registered mocks. Avoid Laravel’s `MockBuilder` for `league/container`.
Can I use league/container alongside Laravel’s container?
Yes, via PSR-11 delegate containers. Create a `league/container` instance and delegate specific services to it while keeping Laravel’s container for framework tasks. Example: `$laravelContainer->delegate(MyService::class, $leagueContainer)`. This is ideal for microservices or CLI tools where you want to separate concerns.
What’s the performance difference between league/container and Laravel’s container?
league/container is optimized for simplicity and speed, with minimal overhead. Laravel’s container includes optimizations for framework use cases (e.g., lazy loading, singleton caching). Benchmark both for your workload—league/container may outperform Laravel’s container for lightweight services, but Laravel’s is tuned for its ecosystem.
Does league/container support scoped bindings (e.g., request-scoped services)?
No, league/container lacks Laravel’s scoped bindings (e.g., `app()->when()` or request scopes). For scoped services, use a hybrid approach: register the service in Laravel’s container with scopes, then delegate resolution to `league/container` for non-scoped dependencies. Alternatively, implement custom logic to manage scopes manually.
How do I handle facades if I switch to league/container?
Facades like `Cache::class` rely on Laravel’s container. To use `league/container`, replace facades with direct service resolution (e.g., `$container->get(Cache::class)`) or create PSR-11-compliant facades. For partial migration, delegate facade services to `league/container` while keeping Laravel’s container for framework facades.
Are there alternatives to league/container for PSR-11 in Laravel?
Yes, other PSR-11 containers include `php-di/php-di` (feature-rich, with autowiring) and `symfony/dependency-injection` (heavier but Laravel-compatible). `league/container` stands out for its minimalism and alignment with PHP standards. Choose based on needs: `league/container` for simplicity, `php-di` for autowiring, or `symfony` for enterprise features.
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.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope