In this Laravel container tutorial, I will explain what is service container in Laravel. The Laravel service container is a powerful tool to manage class dependencies and perform dependency injection. Now the question is what is dependency injection in programming?

laravel-service-container-explanation

Let's take a simple example:

app\Http\Controllers\CountryController.php 

<?php

namespace App\Http\Controllers;

use App\Models\Country;
use Illuminate\Http\Request;

class CountryController extends Controller
{
    public function index(Request $request, Country $country)
    {

    }
}

 

In this example, the Request and Country class is automatically injected without any declaration but it is managing the injection of these dependencies behind the scenes by Laravel. 

Assume in your Country model has a method like:

app\Models\Country.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Country extends Model
{
    use HasFactory;

    protected $fillable = [
        'name'
    ];

    public function getCountryList() : Collection
    {
        return $this::all();
    }
}

 

Now in our country controller index() method, we can automatically call this getCountryList() method by injecting the Country class with this method injection.

app\Http\Controllers\CountryController.php

<?php

namespace App\Http\Controllers;

use App\Models\Country;
use Illuminate\Http\Request;

class CountryController extends Controller
{
    public function index(Request $request, Country $country)
    {
        return $country->getCountryList();
    }
}

 

This is called dependency injection or you can say the method dependency injection and this automatic injection is handled by the Laravel service container. Even though we never have to interact with the container to write this code, before using it. Think about how powerful it is to manage your dependency injection. 

What if when you manually interact with the container?

To know this situation better, first of all, assuming we write a class that implements an interface and we wish to type-hint that interface on a route or class constructor, we must tell the container how to resolve that interface. How can we do that? We have to bind this class with the dependent interface in service providers. 

Laravel service provider, we always have access to the container via the $this->app property. We can register a binding using the bind method, passing the class or interface name to resolve the injection.

app\Providers\AppServiceProvider.php

<?php

namespace App\Providers;

use App\Services\Transistor;
use App\Services\PodcastParser;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(Transistor::class, function ($app) {
            return new Transistor($app->make(PodcastParser::class));
        });
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

 

Remember: There is no need to bind classes into the container if they do not depend on any interfaces.

 

Read also: Central Place Of Laravel | Service Provider Explanation

 

You can learn more about laravel service container from the official documentation of Laravel container. Hope it can help you.