How to create custom Service Provider in Laravel 8

In Laravel application, you have noticed different ServiceProviders class in app/Providers. Service Providers are the central points of all Laravel application bootstrapping. In general, bootstrapping means registering all routes, middlewares, events etc.

If you have seen providers class, these are extending to Illuminate\Support\ServiceProvider class. Most service providers contains register and boot method. 

In this article. we will go through how to write your own service providers and register them with your Laravel application.

Create service providers

Run the below artisan command in Terminal to create a new provider.

php artisan make:provider SiteServiceProvider

This will create new provider class at app/Providers/SiteServiceProvider.php file.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

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

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

Register service provider

As we have new created provider class, but the application doesn't know about it. So we need to tell application about new provider. If you see config/app.php file, you will see $providers array with list of all provider class. We have to add new provider class in this array.

<?php

return [

    'providers' => [
        ...

        App\Providers\SiteServiceProvider::class,
        
        ...
    ]
]

Use of Service Provider

The class generally includes two methods, register method and boot method. In register method, we can implementation of App\Repo\SiteRepo in the service container. In the register method, you will always have access of $app property which provides access for service container.

<?php

namespace App\Providers;

use App\Repo\SiteRepo;
use Illuminate\Support\ServiceProvider;

class SiteServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('SiteRepo', function($app) {
            return new SiteRepo();
        });
    }
}

So we have registered SiteRepo class, so now we can use this class. So we need to create this class. A details method will return server details for the website.

<?php

namespace App\Repo;

use App\Repo\SiteRepo;

class SiteRepo
{
    /**
     * return site details
     *
     * @return void
     */
    public function details()
    {
        return [
            'server' => 'AWS',
            'type' => 'dedicated',
            'disk' => '1250Mb',
        ];
    }
}

So we can now use SiteRepo class in controller file like this.

<?php

namespace App\Http\Controllers;
  
use App\Repo\SiteRepo;
use App\Http\Controllers\Controller;

class ServerController extends Controller
{
    public function index(SiteRepo $siterepo)
    {
        $server_details = $siterepo->details();

        dd($server_details);
    }
}

You should have created route in routes/web.php file to access details.

<?php

use App\Http\Controllers\ServerController;

Route::get('server-details', [ServerController::class, 'index'])->name('index');

So now when you visit this route, you will get server details as defined in SiteRepo class. This was basic knowledge on how to create and register custom provider in Laravel application.