- What is symfony/dependency-injection used for?
- It is a standalone PHP service container that centralizes object construction and dependency wiring. You define services in PHP, XML, YAML, or attributes, and the container resolves dependencies, often through autowiring. Symfony uses it as a core framework component, but it works in any PHP project, including non-Symfony code.
- Is symfony/dependency-injection compatible with Laravel?
- Yes, it can be installed with Composer and used inside a Laravel application, but Laravel already ships illuminate/container with bindings, singletons, contextual binding, and facades. Using Symfony DI in Laravel means running a second container, so you need a service provider bridge and a clear ownership boundary. It is most justified when a package must also run outside Laravel or needs compiler passes and tagged iterators.
- How do I install and configure symfony/dependency-injection in Laravel?
- Run composer require symfony/dependency-injection, then create a ContainerBuilder in a service provider or package bootstrap. Define services in PHP, YAML, XML, or attributes, compile the container, and expose needed entries through Laravel bindings or aliases. For production, dump the compiled container and load the cached file instead of rebuilding it on every request.
- Should I replace Laravel's container with Symfony DependencyInjection?
- Usually no. Laravel's illuminate/container is tightly integrated with facades, service providers, middleware, and the framework lifecycle, so replacing it wholesale is high-risk. Use Symfony DI only for an isolated module or framework-agnostic package, and keep Laravel's container as the primary application container.
- Which PHP and Laravel versions does symfony/dependency-injection support?
- The component follows Symfony's release matrix, so support depends on the Symfony branch you choose, such as 6.4 LTS, 7.x, or 8.x. It has no Laravel-specific version constraint; Laravel compatibility depends on whether your Laravel version supports the PHP version required by that Symfony branch. Check the composer.json constraints before locking versions.
- How does autowiring work in symfony/dependency-injection?
- Autowiring uses constructor type-hints and service definitions to resolve dependencies automatically during container compilation. You still define services or use resource loading so the container knows which classes are services. If a dependency is ambiguous or scalar, you provide explicit arguments, aliases, or bindings.
- What are tags and compiler passes in Symfony DependencyInjection?
- Tags mark services for a purpose, such as event listeners or commands, and compiler passes process those tagged services at build time. Compiler passes let you modify definitions, collect services into iterators, or validate wiring before the container is dumped. They are a key reason to choose Symfony DI over simpler containers when you need extensible package wiring.
- How do I compile and cache the Symfony DI container for production?
- Build a ContainerBuilder, load your configuration, call compile, then dump the container with PhpDumper to a PHP cache file. In production, load the dumped container rather than recompiling on each request. Invalidate that cache when service definitions, environment variables, or package config change, especially across deploys.
- How do I test services wired with Symfony DependencyInjection?
- Build a ContainerBuilder in the test, override service definitions with test doubles, and compile it to verify the wiring. For integration tests, use the same configuration as production but replace external services with stubs. Avoid relying on the dumped production container in unit tests unless you deliberately test the compiled wiring.
- What are alternatives to symfony/dependency-injection and is it actively maintained?
- Laravel's built-in illuminate/container is the default choice for Laravel-only applications, while PHP-DI and league/container are lighter PSR-11 alternatives. Symfony DI is actively maintained as part of Symfony's release cycle, with regular bug and security fixes across supported branches. Choose it when compiler passes, tagged iterators, lazy services, or framework-agnostic wiring justify the extra container. For simple Laravel apps, the native container is usually simpler and safer.