w3resource

Laravel (5.7) Service Container

The Service Container in Laravel is a Dependency Injection Container and a Registry for the application. The advantages of using a Service Container over creating manually your objects are:

Capacity to manage class dependencies on object creation:

You define how an object should be created in one point of the application (the binding) and every time you need to create a new instance, you just ask it to the service container, and it will create it for you, along with the required dependencies For example, instead of creating objects manually with the new keyword:

//everytime we need YourClass we should pass the dependency manually
$instance = new YourClass($dependency);

Instead, you can register a binding on the Service Container:

//add a binding for the class YourClass 
App::bind( YourClass::class, function()
{
    //do some preliminary work: create the needed dependencies
    $dependency = new DepClass( config('some.value') );

    //create and return the object with his dependencies
    return new YourClass( $dependency );
});

and create an instance through the service container with:

//no need to create the YourClass dependencies, the SC will do that for us!
$instance = App::make( YourClass::class );

With Laravel automatic dependency injection, when an interface is required in some part of the app (i.e. in a controller's constructor), a concrete class is instantiated automatically by the Service Container. Changing the concrete class on the binding, will change the concrete objects instantiated through all your app:

//every time a UserRepositoryInterface is requested, create an EloquentUserRepository 
App::bind( UserRepositoryInterface::class, EloquentUserRepository::class );
//from now on, create a TestUserRepository 
App::bind( UserRepositoryInterface::class, TestUserRepository::class );

Using the Service Container as a Registry

You can create and store unique object instances on the container and get them back later: using the App::instance method to make the binding, and thus using the container as a Registry.

// Create an instance.
$kevin = new User('Kevin');


// Bind it to the service container.
App::instance('the-user', $kevin);


// ...somewhere and/or in another class...

// Get back the instance
$kevin = App::make('the-user');

As a final note, essentially the Service Container -is- the Application object: it extends the Container class, getting all the container's functionalities.

Previous: Laravel (5.7) Request Lifecycle
Next: Laravel (5.7) Service Providers



Follow us on Facebook and Twitter for latest update.