What dependency injection methods are available in Laravel?
Asked at
AI answer
The primary approach is constructor injection: Laravel automatically resolves dependencies registered with, or automatically created by, the container. Dependencies can also be injected into controller methods, regular methods, commands and handlers, as well as route handlers. The container is configured using bindings (bind, singleton, scoped), interfaces, and contextual bindings. Facades provide access to services, but they are not usually considered dependency injection: they are a static interface to the container.
Also asked at SkyCoach
Similar questions
- What is the difference between lazy loading and eager loading in Laravel?
- What is Eloquent? What are its pros and cons, and when should you use it versus avoid it?
- Which design patterns have you used in real-world work, and in what situations?
- What is Dependency Injection and why is it needed?
- In PHP, can you swap characters/variables without allocating a `$temp` variable and without using functions?