Laravel Custom Service Provider Example

Jun 14, 2021 . Admin

Hi Dev,

Today i will share you how to create custom service provider in laravel.The laravel custom service provider tutorial is so easy to utilize.so you can just follow my steps.

So let's start to the example and follow to the my all step.

Now, It looks great, doesn’t it? There is no charm here, you can look at the contents of file `config/app.php`. You’ll find an array which used to declare all service providers, These service providers will be loaded during the bootstrapping of the Laravel project.

Example
Path : config/app.php
'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,
/*
    * Application Service Providers...
    */
   App\Providers\AppServiceProvider::class,
   App\Providers\AuthServiceProvider::class,
   App\Providers\EventServiceProvider::class,
   App\Providers\RouteServiceProvider::class,
/**
    * Custom Provider
    */
],
Create service provider
php artisan make:provider CustomserviceController

So, all service providers extend the Illuminate\Support\ServiceProvider class. Most service providers contain a register and a boot method. Within the register method, you should only bind things into the service container. You should never attempt to register any event listeners, routes, or any other piece of functionality within the register method.

Path : app/Providers/CustomserviceController.php
<?php

namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class CustomserviceController extends ServiceProvider
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function boot()
    {
     //
    }

    /**
     * Write code on Method
     *
     * @return response()
     */
    public function register()
    {
     //
    }
}

So, we go ahead with an example which uses interface, so let engender your interface.

<?php

namespace App\Service;
interface AwesomeServiceInterface
{
    public function doAwesomeThing();
}

And the service

<?php

namespace App\Service;
class AwesomeService implements AwesomeServiceInterface
{
    public function doAwesomeThing()
    {
        echo ‘do awesome thing !!!’;
    }
}

After that, instead of binding a class, we’ll bind an interface.

<?php

namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class CustomserviceController extends ServiceProvider
{   
    public function boot()
    {
        //
    }
    
    /**
     * Write code on Method
     *
     * @return response()
     */    
    public function register()
    {
        $this->app->bind(‘App\Service\AwesomeServiceInterface’, ‘App\Service\AwesomeService’);
    }
}

Add your service provider into file config/app.php

Path : config/app.php
‘providers’ => [
    ……
    /**
    * Custom Provider
    */
    App\Providers\CustomserviceController::class,
],

Let create controller to test your service

Path : app/Http/Controllers/TestController.php
<?php

namespace App\Http\Controllers;
use App\Service\AwesomeServiceInterface;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

/**
 * Write code on Method
 *
 * @return response()
 */
class TestController extends Controller
{
     public function doAwesome(AwesomeServiceInterface $awesome_service)
     {
         $awesome_service->doAwesomeThing();
         return new Response();
     }
}

It Will Help You...

#Laravel 8 #Laravel