How To Create Custom Service Provider In Laravel

10-Mar-2023

.

Admin

How To Create Custom Service Provider In Laravel

Hi guys,

Today i will explained to the how to create custom service provider in laravel.The laravel custom service provider tutorial is so easy to use.so you can just follow my step by step and learn laravel custom getter tutorial.

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

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 application.

Solution


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 AwesomeServiceProvider

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.

app/Providers/AwesomeServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AwesomeServiceProvider extends ServiceProvider

{

public function boot()

{

//

}

public function register()

{

//

}

}

Now, we go ahead with an example which uses interface, so let create 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 AwesomeServiceProvider extends ServiceProvider

{

public function boot()

{

//

}

public function register()

{

$this->app->bind(‘App\Service\AwesomeServiceInterface’, ‘App\Service\AwesomeService’);

}

}

Add your service provider into file config/app.php

config/app.php

‘providers’ => [

……

/**

* Custom Provider

*/

App\Providers\AwesomeServiceProvider::class,

],

Let create controller to test your service

app/Http/Controllers/TestController.php

<?php

namespace App\Http\Controllers;

use App\Service\AwesomeServiceInterface;

use Illuminate\Http\Request;

use Illuminate\Http\Response;

class TestController extends Controller

{

public function doAwesome(AwesomeServiceInterface $awesome_service)

{

$awesome_service->doAwesomeThing();

return new Response();

}

}

#Laravel 8

#Laravel 7

#Laravel