If you are using Laravel for a long time then you also came across the Laravel Service Providers and Service Containers. Honestly telling you that they are the backbone of the Laravel Framework. Even your whole Laravel Application's core services are bootstrapped via. service providers. In this section, I will only talk about the Laravel Service Provider.

If you open config/app.php file, you will see the list of service providers registered in providers array. Below is the look of providers array.

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Notifications\NotificationServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,

        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,

    ],

All of these are service providers, but there is one thing that not all services are loaded in each request which is known as 'deferred' providers. These providers will execute when we want.

How to write your own Service Provider in Laravel?
Laravel has given a very useful command-line feature to create a service provider. Because time is valuable for everyone so no one wants to rewrite the whole code template again and again. Now open command line console and type the artisan command.

php artisan make:provider DemoServiceProvider

After running this command your provider will be created in app/Providers directory. You also need to register your provider in the providers' array that we talked about earlier.

'providers' => [
        ...

        /* here we registering our provider */
        App\Providers\DemoServiceProvider::class

        ...
 ]


Now let's review the DemoServiceProvider.php file.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class DemoServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

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

In each service provider, you will find two common methods i.e. register() and boot(). In register() method we only bind service containers. We should not write any functionality in the register() method because according to Laravel Request Life Cycle, register() method will call on each provider first, and then boot method will call.

If you want to add something then boot() method is the right place because all services are loaded and you can use any service you want.

So, now we have created our service provider. This service provider will automatically hit on each request. For proof you can dd('anything here'); //dump and die in register(). You will get the output which clearly signifies that your request will die at the time of registering the provider.

Conclusion

As of now, if you want any service that can be load on each request then you can go for the service providers. This is just starting overview for Service Provider. In the next section, we will use Service Container and will combine it with Service Provider because these two are related to each other and both things in same section will make this post too long which is an overhead.

Last Modified: 5 months ago