0

I just learned about Laravel Service Containers and it seems like a great functionality. as I understood it prevents us from rewriting 50 controllers when we should modify a commonly used entity or variable throughout the project.

the problem being is that I don't see a proper Use case for this feature, I mean if you have a piece of data or an entity that you're repeatedly using: this can be customized through a model

so when should I use service containers in laravel ? what are the Pros and cons of this functionality ?

1 Answers1

4

as I understood it prevents us from rewriting 50 controllers when we should modify a commonly used entity or variable throughout the project.

I do not believe you understand it correctly.

Service Containers are just a fancy term that Laravel came up with to describe dependency injection. The major benefit is for unit testing and its biggest competitor is the facade pattern that Laravel also uses. The biggest benefit of dependency injection is that you can mock expectations without requiring additional "scaffolding" code that bootstraps the test. More information about using dependency injection for unit testing: https://medium.com/philipobenito/dependency-injection-as-a-tool-for-testing-902c21c147f1

Alex Barker
  • 4,316
  • 4
  • 28
  • 47
  • 2
    For me the most amazing aspect of the service container is that you can bind a service to an interface implementation. This allows you to chage implementation without having to touch the rest of your code, by just replacing the binding of the interface to the new class. This is for me the part I like the most. Maybe you can add that to your answer which is very valid so we don't have 2 answers :) – Eli Nov 14 '19 at 20:00