0

As I understand, the main point of Laravel's service providers and service container is to allow for DI. However Laravel also has "automatic resolution" whereby the framework appears to automatically inject any dependencies a class needs.. therefore when would you need to use a service provider and the service container?

jon
  • 1,429
  • 1
  • 23
  • 40
  • 1
    The service providers tell the IoC (the thing that does the automatic resolution) how to inject those dependencies, and handles any preloading necessary before it does so. So for example, if you want the app to load a specific configuration defined by the ServiceProvider and not hard coded into the class itself. – jardis May 10 '16 at 18:25
  • @Emn1ty, thanks for your comment. So basically if the class needs extra config then you would need to use a service provider, otherwise there would be no real need? (Alternatively, the config could be left in the class itself) – jon May 10 '16 at 20:13
  • 1
    Yes, but the whole idea of DI is to inject the necessary dependancies, so configuration could easily fall into that, since the class should not be responsible for grabbing its configuration. It should be provided to it. It also allows for something called Interface Injection, where you can use an agnostic Interface class that can then have different implementations easily swapped out using the service providers. So if you had a version that used Eloquent, and another that used ActiveRecord or something it would be as simple as changing the service provider to switch between them. – jardis May 10 '16 at 21:48

0 Answers0