Discover how a simple change can make your Laravel controllers cleaner and your app easier to grow!
Why Dependency injection in controllers in Laravel? - Purpose & Use Cases
Imagine building a web app where your controller needs to create and manage many objects manually, like database connections or service classes, every time a user makes a request.
Manually creating these objects in controllers leads to messy code, repeated work, and makes it hard to change or test parts of your app without breaking others.
Dependency injection lets Laravel automatically provide the needed objects to your controller, keeping your code clean, easy to read, and simple to test.
public function __construct() { $this->service = new UserService(); }public function __construct(UserService $service) { $this->service = $service; }This makes your controllers flexible and your app easier to maintain and extend over time.
When building a blog, injecting a PostRepository into your controller means you can swap how posts are fetched without changing the controller code.
Manual object creation clutters controllers and causes tight coupling.
Dependency injection provides needed objects automatically.
It improves code clarity, testing, and flexibility.
Practice
Solution
Step 1: Understand dependency injection concept
Dependency injection means Laravel automatically gives the controller the objects it needs without manual creation.Step 2: Identify the purpose in Laravel controllers
This keeps controllers clean and easy to test by providing dependencies automatically.Final Answer:
To automatically provide required objects to the controller -> Option AQuick Check:
Dependency injection = automatic object provision [OK]
- Thinking you must create objects manually inside controllers
- Confusing dependency injection with global variables
- Believing dependency injection removes use of external classes
Solution
Step 1: Check constructor parameter type hinting
public function __construct(Service $service) { $this->service = $service; } correctly type hints the Service class in the constructor parameter.Step 2: Verify assignment to class property
public function __construct(Service $service) { $this->service = $service; } assigns the injected service to a class property for later use, which is correct.Final Answer:
public function __construct(Service $service) { $this->service = $service; } -> Option BQuick Check:
Constructor injection = type hint + assign property [OK]
- Not type hinting the service class in constructor
- Creating new service instance inside constructor instead of injecting
- Assigning class name string instead of object
public function show(Service $service) {
return $service->greet();
}Solution
Step 1: Understand method injection
The Service object is injected automatically into the show method by Laravel.Step 2: Call the greet() method on the injected service
The greet() method returns the string "Hello", so the method returns "Hello".Final Answer:
"Hello" -> Option AQuick Check:
Injected service method output = "Hello" [OK]
- Assuming service is not injected and causes error
- Expecting method name instead of method return value
- Confusing null with empty string output
public function __construct(Service $service) {
$service = new Service();
$this->service = $service;
}Solution
Step 1: Analyze the constructor parameter and assignment
The constructor receives a Service object but immediately overwrites it by creating a new instance.Step 2: Understand why this is wrong
This defeats dependency injection because the injected object is ignored and a new one is created manually.Final Answer:
It creates a new Service instance instead of using the injected one -> Option CQuick Check:
Overwriting injected service = wrong [OK]
- Overwriting injected parameter with new instance
- Forgetting to assign injected object to property
- Confusing type hinting with variable assignment
Logger and Mailer, into a Laravel controller and use them in a method. Which constructor and method setup is correct?Solution
Step 1: Check constructor injection of both services
public function __construct(Logger $logger, Mailer $mailer) { $this->logger = $logger; $this->mailer = $mailer; } public function notify() { $this->logger->log('Notify'); $this->mailer->send(); } correctly injects both Logger and Mailer in the constructor and assigns them to properties.Step 2: Verify method usage of injected services
The notify method uses the injected properties to call log and send methods, which is correct.Final Answer:
public function __construct(Logger $logger, Mailer $mailer) { $this->logger = $logger; $this->mailer = $mailer; } public function notify() { $this->logger->log('Notify'); $this->mailer->send(); } -> Option DQuick Check:
Inject all services in constructor, use properties in methods [OK]
- Injecting services only in methods instead of constructor
- Creating new instances inside constructor instead of injecting
- Mixing constructor and method injection inconsistently
Ready to prove it?
Pass the test and this pattern moves unknown
