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
Dependency Injection

Dependency Injection Laravel Package

symfony/dependency-injection

Symfony DependencyInjection standardizes and centralizes object construction with a powerful service container. Define, configure, and wire services, manage parameters and service lifecycles, and enable autowiring and compilation for efficient, testable apps.

View on GitHub
Deep Wiki
Context7

Allows you to standardize and centralize the way objects are constructed in your application

Frequently asked questions about Dependency Injection
How does Symfony DependencyInjection integrate with Laravel’s Service Container?
Laravel’s Service Container (Illuminate/Container) is built on top of Symfony DependencyInjection, so integration is seamless. Laravel’s `bind()`, `singleton()`, and `tag()` methods are direct wrappers for Symfony’s container features. You can use Symfony’s advanced DI capabilities like CompilerPasses, autowiring, and attributes (e.g., #[Autowire]) without extra setup.
Can I use Symfony’s CompilerPasses in Laravel?
Yes, Laravel fully supports Symfony CompilerPasses. You can extend Laravel’s container by adding custom passes in a Service Provider’s `register()` method using `$this->app->make('container')->addCompilerPass()`. This is useful for dynamic service registration or pre-processing definitions before compilation.
What Laravel versions support Symfony DependencyInjection v6.3+?
Laravel 10+ uses Symfony DependencyInjection v6.3+ by default. For Laravel 9, you’ll need to manually pin to a compatible version (e.g., `^6.3`). Always check Laravel’s documentation for version-specific requirements, as older versions may have breaking changes.
How do I enable autowiring for classes in Laravel using Symfony DI?
Laravel’s autowiring is powered by Symfony’s autoconfiguration. Enable it in `config/app.php` by setting `'autowire' => true` and `'autowire_realtime' => true`. For custom classes, use Symfony’s attributes like `#[Autowire]` or configure autowiring in a Service Provider with `$container->autowire()`.
What’s the best way to debug Symfony DI issues in Laravel?
Use Laravel’s `dd($container->getServiceIds())` to inspect registered services. For deeper debugging, enable Symfony’s debug mode by setting `'debug' => true` in `config/app.php`. Check the container’s `ParameterBag` for configuration issues, and use `php artisan optimize:clear` to reset compiled services.
Can I replace manual `new` calls with Symfony DI in Laravel?
Absolutely. Replace `new Logger()` with `$container->get(LoggerInterface::class)` or use Laravel’s `app()` helper (e.g., `app(LoggerInterface::class)`). For classes without bindings, use `bind()` in a Service Provider or leverage autowiring. This improves testability and modularity.
How do I use Symfony’s service tags in Laravel?
Laravel’s `app()->tagged('mailers')` works with Symfony’s tagged services. Define tags in a Service Provider using `$container->setAlias()` or attributes like `#[AsTaggedItem('mailers')]`. Access tagged services via `app()->tagged('mailers')` or `app()->makeWithTag('mailers')`.
Will custom CompilerPasses slow down Laravel’s optimization?
CompilerPasses can impact performance if overused, especially during `php artisan optimize`. Profile with Laravel Debugbar or Xdebug to identify bottlenecks. Use passes sparingly for critical tasks like dynamic binding or avoid them entirely if performance is a concern.
Are there alternatives to Symfony DI for Laravel?
Laravel’s built-in container is sufficient for most use cases, but Symfony DI offers advanced features like CompilerPasses, attributes, and fine-grained control. For lightweight alternatives, consider PHP-DI or League Container, though they lack Laravel’s deep integration. Symfony DI is the most robust choice for complex DI needs.
How do I test services managed by Symfony DI in Laravel?
Mock dependencies by overriding the container’s definitions in tests. Use `$container->set()` to replace services or leverage Laravel’s `MockApplication` for isolated testing. For autowired services, use `createMock()` or `Mockery` to stub collaborators. Symfony’s `ParameterBag` can also be inspected for configuration.
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