0

Where can I find the Service Container in a Laravel project.

All the Service Providers are in single location, in the app/Providers directory, but where to find the Service Container?

3
  • Are you asking for where to find the source code of the container or how to access to an instance of the container?. If the later, function app() will give you an instance of the container Commented Aug 30, 2018 at 4:14
  • i think you can find something here [laravel.com/docs/5.6/container] Commented Aug 30, 2018 at 4:24
  • @JaviStolz , What does the "container" in the "Service Container" mean? Is it some directory, some php file, or some php class? What is it? Commented Aug 30, 2018 at 4:51

1 Answer 1

5

Service container is a core component of Laravel framework that is ready for you to use as it is, you are not supposed to create your own service containers like you do, for example, with service providers.

You can imagine service container being an associative array where you can store dependencies (services) and logic of how to resolve them. Then you can use service container to give you what you need based on what is there using provided logic.

It would be easier to imagine that service container is a black box that is always available. Your app at first registers (puts) certain rules in there (for example, if someone wants an object that implements PriceCalculator interface then give him object of a class MyPriceCalculator). It is done in register() method of your service providers:

$this->app->bind('App\Contracts\PriceCalculator', 'App\Shop\MyPriceCalculator');

Then this black box is always available for you, so if you ever need PriceCalculator object (for example, somewhere in your cart controller to calculate price of some order) you can now instead of doing:

$calculator = new \App\Shop\MyPriceCalculator;

Ask service container to make you a proper one:

$calculator = app()->make('App\Contracts\PriceCalculator');

Note how we are asking service container to give us implementation of an interface and will in turn give us new App\Shop\MyPriceCalculator object because that is how we defined (registered) App\Contracts\PriceCalculator service earlier.

Using service container is a great way to manage all dependencies of your application since your code will be working with abstractions and how those abstractions are resolved will be always defined in one place (which means it's easier to maintain if you want to change something later on).

If you are new to Laravel I would recommend to skip service containers for now since it's a little bit more advanced topic and you are required to have a better understanding of dependency injection pattern to fully grasp it and use properly.

You can read official documentation here.

Sign up to request clarification or add additional context in comments.

2 Comments

So the "Service container" is used only to register and resolve the Services? And it doesn't hold the actual "Service codes"? If so, then where should we put our Service code ? In which folder ?
Yes, it doesn't hold any "service codes" (which are essentially just your classes). As to where to put them you are free to decide yourself. Some classes will be inside packages you install, some will be written by yourself. Usually I try to group them up in app\Services folder. Note that you can resolve any class via service container, even if you didn't register it explicitly. You can always app()->make('App\User') to create a new eloquent object of user. Then if you ever need to extend that class you can register in container that when App\User is needed give App\ExtendedUser etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.