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

Di Laravel Package

yiisoft/di

PSR-11 compatible dependency injection container for PHP 8.1+. Supports autowiring plus constructor, method and property injection, aliasing, service providers, delegated/composite containers, circular reference detection, and state reset for long-running workers.

View on GitHub
Deep Wiki
Context7

yiisoft/di is a PSR-11 compatible dependency injection container that instantiates and configures objects while automatically resolving dependencies. It supports modern DI patterns and is designed to work well with mergeable configurations and long-running PHP workers.

Key features:

  • Autowiring with constructor, method, and property injection
  • Circular reference detection to prevent runtime loops
  • Array-based definitions for simple, mergeable configs
  • Aliasing, delegated lookup, and composite container support
  • State resetter for long-running processes (e.g., RoadRunner, Swoole)
Frequently asked questions about Di
Can I use yiisoft/di as a drop-in replacement for Laravel’s built-in service container?
No, it’s not a direct drop-in due to method naming differences (e.g., `set()` vs. `bind()`), but you can integrate it alongside Laravel’s container. Replace the underlying implementation by binding Yii’s container to Laravel’s `AppServiceProvider` or use it for specific services via `app()->set()`.
How do I configure yiisoft/di to work with Laravel’s Facades or Service Providers?
Yii’s container works alongside Laravel’s Facades and Service Providers without modification. Register services using array definitions (e.g., `['class' => MyService::class, 'params' => [...]]) via `app()->set()` or `Container::getInstance()->set()`. Facades will resolve dependencies normally if the container is properly bound.
Does yiisoft/di support Laravel’s contextual binding (e.g., binding interfaces to classes dynamically)?
Yii’s container supports contextual binding natively, but Laravel’s container lacks this out-of-the-box. You can emulate it by using closures or middleware in Laravel’s `bind()` method, or extend the container with custom logic to delegate to Yii’s container for dynamic resolution.
Will yiisoft/di break Laravel’s caching (e.g., config caching or route caching)?
No, it won’t break Laravel’s caching systems. However, if you use array-based definitions, ensure they’re serialized correctly for cached configurations. Yii’s container supports mergeable configs, which can coexist with Laravel’s cached bindings if properly integrated.
How do I handle circular dependencies in Laravel with yiisoft/di?
Yii’s container detects circular references automatically and throws an exception if found. To use it in Laravel, bind your services to the Yii container (e.g., `app()->set('service', [...])`) and let it handle dependency resolution. Circular references will be caught at runtime with clear error messages.
Can I use yiisoft/di in Laravel for long-running processes like Swoole or RoadRunner?
Yes, Yii’s container includes a **state resetter** specifically designed for long-running workers. Initialize the container once and reset its state between requests to avoid memory leaks. This is ideal for Laravel apps using Swoole or RoadRunner as the server.
What’s the performance impact of replacing Laravel’s container with yiisoft/di?
Yii’s container is lightweight (~500 LOC) and optimized for performance, with minimal overhead. Benchmarks show it’s comparable to Laravel’s container, but the real benefit comes from features like circular reference detection and mergeable configs, which reduce boilerplate and improve maintainability.
How do I migrate from Laravel’s container to yiisoft/di incrementally?
Start by registering critical services in Yii’s container (e.g., `app()->set('service', [...])`) while keeping Laravel’s container for the rest. Gradually replace bindings as you test. Use Laravel’s `app()->bind()` for hybrid setups until full migration is complete.
Does yiisoft/di support Laravel’s Eloquent ORM or Query Builder?
Yes, Yii’s container works seamlessly with Eloquent and Laravel’s Query Builder. Bind Eloquent models or repositories using array definitions (e.g., `['class' => UserRepository::class, 'params' => ['model' => User::class]]) and resolve them like any other service.
Are there alternatives to yiisoft/di for PSR-11 containers in Laravel?
Yes, alternatives include `php-di/php-di` (feature-rich but heavier) and `league/container` (simpler but less advanced). Yii’s container stands out for its Yii heritage, circular reference detection, and support for long-running workers, making it ideal for performance-sensitive Laravel apps with complex DI needs.
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