What is dependency injection in Laravel controllers?
Dependency injection is a way to automatically provide a controller with the objects it needs, like services or repositories, without creating them inside the controller. Laravel does this by passing the needed objects through the controller's constructor or methods.
Click to reveal answer
beginner
How do you inject a service into a Laravel controller constructor?
You add the service type as a parameter in the controller's constructor. Laravel's service container will automatically create and pass the service instance when the controller is used.
Click to reveal answer
intermediate
Why is dependency injection better than creating objects inside controllers?
It makes the code cleaner, easier to test, and more flexible. You can change the service without changing the controller, and Laravel handles creating the objects for you.
Click to reveal answer
beginner
Show a simple example of injecting a repository into a Laravel controller.
class UserController {
protected $userRepo;
public function __construct(UserRepository $userRepo) {
$this->userRepo = $userRepo;
}
public function index() {
return $this->userRepo->allUsers();
}
}
Click to reveal answer
intermediate
Can Laravel inject dependencies into controller methods? How?
Yes, Laravel can inject dependencies directly into controller methods by type-hinting the parameters. Laravel will resolve and pass the needed objects automatically when the method is called.
Click to reveal answer
What does Laravel use to automatically provide dependencies to controllers?
AStatic methods
BManual object creation
CGlobal variables
DService container
✗ Incorrect
Laravel uses a service container to automatically create and inject dependencies into controllers.
Where do you usually declare dependencies for injection in a Laravel controller?
AIn the constructor parameters
BInside the controller methods without parameters
CIn the routes file
DIn the blade templates
✗ Incorrect
Dependencies are usually declared as parameters in the controller's constructor for injection.
What is a key benefit of using dependency injection in controllers?
ASlower performance
BEasier testing and cleaner code
CMore global variables
DHarder to change services
✗ Incorrect
Dependency injection makes code easier to test and cleaner by separating concerns.
Can Laravel inject dependencies into controller methods directly?
AYes, by type-hinting method parameters
BNo, only in constructors
COnly if you use static methods
DOnly in middleware
✗ Incorrect
Laravel supports method injection by type-hinting parameters in controller methods.
What happens if Laravel cannot resolve a dependency in a controller?
AIt creates a null object
BIt ignores the dependency
CIt throws an error
DIt uses a default global instance
✗ Incorrect
If Laravel cannot resolve a dependency, it throws an error indicating the missing binding.
Explain how dependency injection works in Laravel controllers and why it is useful.
Think about how Laravel provides needed objects without manual creation.
You got /4 concepts.
Describe how you would inject a repository into a Laravel controller and use it in a method.
Focus on constructor injection and accessing the injected object.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of dependency injection in Laravel controllers?
easy
A. To automatically provide required objects to the controller
B. To manually create objects inside controller methods
C. To make controllers dependent on global variables
D. To avoid using any external classes in controllers
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 A
Hint: Dependency injection means Laravel gives needed objects automatically [OK]
Common Mistakes:
Thinking you must create objects manually inside controllers
Confusing dependency injection with global variables
Believing dependency injection removes use of external classes
2. Which of the following is the correct way to inject a service into a Laravel controller constructor?
easy
A. public function __construct($service) { $this->service = new Service(); }
B. public function __construct(Service $service) { $this->service = $service; }
C. public function __construct() { $this->service = Service::class; }
D. public function __construct(Service $service) { $service = new Service(); }
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 B
Quick Check:
Constructor injection = type hint + assign property [OK]
Hint: Type hint service in constructor and assign to property [OK]
Common Mistakes:
Not type hinting the service class in constructor
Creating new service instance inside constructor instead of injecting
Assigning class name string instead of object
3. Given this controller method, what will be the output if the injected service returns 'Hello' from its greet() method?
public function show(Service $service) {
return $service->greet();
}
medium
A. "Hello"
B. Error: Service not found
C. null
D. "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 A
Quick Check:
Injected service method output = "Hello" [OK]
Hint: Injected method returns service method result directly [OK]
Common Mistakes:
Assuming service is not injected and causes error
Expecting method name instead of method return value
Confusing null with empty string output
4. What is wrong with this controller constructor for dependency injection?
public function __construct(Service $service) {
$service = new Service();
$this->service = $service;
}
medium
A. It lacks type hinting for the service parameter
B. It does not assign the service to a class property
C. It creates a new Service instance instead of using the injected one
D. It should not use $this keyword inside constructor
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 C
Quick Check:
Overwriting injected service = wrong [OK]
Hint: Do not create new instance; use injected object directly [OK]
Common Mistakes:
Overwriting injected parameter with new instance
Forgetting to assign injected object to property
Confusing type hinting with variable assignment
5. You want to inject two services, Logger and Mailer, into a Laravel controller and use them in a method. Which constructor and method setup is correct?
hard
A. public function __construct() { $this->logger = new Logger(); $this->mailer = new Mailer(); } public function notify() { $this->logger->log('Notify'); $this->mailer->send(); }
B. public function __construct(Mailer $mailer) { $this->mailer = $mailer; } public function notify(Logger $logger) { $logger->log('Notify'); }
C. public function __construct(Logger $logger) { $this->logger = $logger; } public function notify(Mailer $mailer) { $mailer->send(); }
D. public function __construct(Logger $logger, Mailer $mailer) { $this->logger = $logger; $this->mailer = $mailer; } public function notify() { $this->logger->log('Notify'); $this->mailer->send(); }
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 D
Quick Check:
Inject all services in constructor, use properties in methods [OK]
Hint: Inject all services in constructor, use $this->property in methods [OK]
Common Mistakes:
Injecting services only in methods instead of constructor
Creating new instances inside constructor instead of injecting
Mixing constructor and method injection inconsistently
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.