Bird
0
0

You need to inject both NotificationService and UserRepository into a Laravel controller and use them in multiple methods. What is the recommended approach?

hard📝 Conceptual Q8 of 15
Laravel - Controllers
You need to inject both NotificationService and UserRepository into a Laravel controller and use them in multiple methods. What is the recommended approach?
AInject only one service via constructor and use Laravel's facade for the other.
BInject <code>NotificationService</code> in the constructor and resolve <code>UserRepository</code> manually inside each method.
CManually instantiate both services inside each controller method where needed.
DInject both services via the constructor and assign them to class properties.
Step-by-Step Solution
Solution:
  1. Step 1: Use constructor injection for both dependencies

    This ensures services are resolved once and available throughout the controller.
  2. Step 2: Assign injected services to class properties

    Allows easy access in any controller method without repeated resolution.
  3. Final Answer:

    Inject both services via the constructor and assign them to class properties. -> Option D
  4. Quick Check:

    Constructor injection for multiple dependencies [OK]
Quick Trick: Inject all dependencies in constructor for reuse [OK]
Common Mistakes:
  • Resolving dependencies manually inside methods
  • Instantiating services directly instead of injecting
  • Mixing injection and facades inconsistently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes