Skip to content

Yish

Laravel Service Container Exploring

Laravel1 min read

The service container is the core of laravel, all of the service providers and facades are binding and aliasing to it, through infrastructure building and clever thinking design we got one of the beautiful and modern PHP frameworks, how does it work?

Tracing underlying codebase

It includes several actions for requesting, http kernel and exception handler, etc, for now, we just focusing service container how does it work.

Next, we're going to Illuminate\Foundation\Application, you're going to see the important things :

Certainly, the Laravel was doing for many things, binding service providers and etc.

Now we're knowing the basic logic of binding, so we can implement simple servicer container by ourselves, here are steps.

For example, we're going to bind GoogleComputeEngine on the container, we created the class Post firstly.

Next, we want to bind the key to the container.

Finally, we resolve the key and execute the closure class.

Fortunately, if you are in Laravel, you can simply do this on service provider:

The Laravel provides many useful methods in container and application, you can refer Service Container - Laravel - The PHP Framework For Web Artisans.

For now, we’re knowing application container includes all binding service providers and key if we don’t want to use a facade to execute methods, how to do this?

You can refer Illuminate\Foundation\Application, check the alias keys or you can navigate to config/app.php aliases facade: