- 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.