0
0
Laravelframework~10 mins

Dependency injection in controllers in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Dependency injection in controllers
Request comes in
Laravel Router matches route
Controller instantiated
Dependencies injected via constructor or method
Controller method executes with dependencies
Response returned
Laravel receives a request, matches it to a route, creates the controller, injects needed dependencies automatically, then runs the controller method.
Execution Sample
Laravel
class UserController {
  protected $repo;

  public function __construct(UserRepository $repo) {
    $this->repo = $repo;
  }
  public function index() {
    return $this->repo->all();
  }
}
This controller receives a UserRepository automatically injected, then uses it to get all users.
Execution Table
StepActionDependencyController StateOutput
1Request received for /usersNone yetNo controller instanceNone
2Route matched to UserController@indexNone yetNo controller instanceNone
3Instantiate UserControllerUserRepository created and injectedrepo = UserRepository instanceNone
4Call index() methodrepo availablerepo = UserRepository instanceCalls repo->all()
5UserRepository->all() returns user listrepo usedrepo unchangedUser list returned
6Response sent to clientrepo usedrepo unchangedUser list JSON
💡 Controller method finishes and response is sent back to client
Variable Tracker
VariableStartAfter Step 3After Step 4Final
reponullUserRepository instanceUserRepository instanceUserRepository instance
Key Moments - 3 Insights
How does Laravel know what to inject into the controller constructor?
Laravel uses type hints in the constructor to automatically create and inject the correct dependency, as shown in step 3 of the execution_table.
What if the dependency requires its own dependencies?
Laravel resolves nested dependencies automatically by checking the constructor of each dependency, so UserRepository's dependencies are also injected before UserRepository is passed to the controller.
Can dependencies be injected into controller methods instead of constructor?
Yes, Laravel supports method injection where dependencies are passed as parameters to controller methods, resolved automatically before method execution (similar to constructor injection shown in step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of the controller after step 3?
Arepo is null
BController is not instantiated
Crepo is a UserRepository instance
Drepo is a string
💡 Hint
Check the 'Controller State' column at step 3 in the execution_table
At which step does Laravel inject the UserRepository dependency?
AStep 1
BStep 3
CStep 2
DStep 5
💡 Hint
Look for when the controller is instantiated and dependencies are created in the execution_table
If the UserRepository had another dependency, how would the execution_table change?
AStep 3 would include creating that dependency first
BStep 4 would inject the new dependency
CStep 1 would create all dependencies
DNo change needed
💡 Hint
Recall Laravel resolves nested dependencies before injecting the main dependency at controller instantiation
Concept Snapshot
Dependency injection in Laravel controllers:
- Laravel auto-injects dependencies by type hinting in constructors or methods.
- Controller is instantiated with dependencies resolved first.
- Dependencies can have their own dependencies, resolved recursively.
- This keeps controllers clean and testable.
- Injection happens before controller method runs.
Full Transcript
When Laravel receives a request, it matches the route to a controller method. Before calling the method, Laravel creates the controller instance. It looks at the constructor's type hints and automatically creates and injects those dependencies. For example, if the controller needs a UserRepository, Laravel creates it and passes it in. Then the controller method runs using that dependency. This process makes code cleaner and easier to test because dependencies are managed automatically. If dependencies have their own dependencies, Laravel resolves those too before injection. This can also happen in controller methods via method injection.