Category : #laravel
Tags : #laravel, #laravel service provider
In this Laravel service provider tutorial, I will explain what actually service provider is and why you need it in your Laravel application. We will also see how to create a custom service provider in Laravel and how to register it in Laravel.
Introduction to Service Provider in Laravel
We can say that the service provider is the backbone of the Laravel application. Now let's see the Laravel definition of a service provider. In Laravel, service providers are the central place of all Laravel application bootstrapping systems. Your own application, as well as all of Laravel's core services, are bootstrapped via service providers.
But do you know what is bootstrapped? In general, we mean registering application classes, including registering service container bindings, event listeners, middleware, and even routes.
If we look at the config/app.php
file included with Laravel, you will see a providers
array.
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,
/*
* 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,
Yajra\DataTables\DataTablesServiceProvider::class,
Yajra\DataTables\ButtonsServiceProvider::class,
browner12\helpers\HelperServiceProvider::class,
OwenIt\Auditing\AuditingServiceProvider::class,
App\Providers\ViewComposerServiceProvider::class,
],
All of those service provider classes will be loaded for your application. The service provider contains two methods, one is the boot() and the other is register(). You will get all the providers in your App\Providers directory.
App\Providers\AppServiceProvide.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
}
}
Create Custome Service Provider
Laravel has Artisan CLI that can generate a new provider via the make:provider
command:
php artisan make:provider RiakServiceProvider
If you run this command, it will create a service provider for you in your provider's directory.
App\Providers\RiskServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class RiskServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
}
}
Now let's see what is the use case of the register
and boot
method.
The Register Method in Service Provider
Remember within the register
method, you can only bind things into the service container. We should not attempt to register any event listeners, routes, or any other piece of functionality within the register
method. Otherwise, it may not be loaded yet.
App\Providers\RiskServiceProvider.php
<?php
namespace App\Providers;
use App\Services\Riak\Connection;
use Illuminate\Support\ServiceProvider;
class RiakServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->singleton(Connection::class, function ($app) {
return new Connection(config('riak'));
});
}
}
The Boot Method in Service Provider
This boot method of the service provider is called after all other service providers have been registered. That mean we have access to all other services that have been registered by the framework:
Register Custom Service Provider
You already know that all the service providers are loaded from the config/app.php file's providers array. So register it like:
config/app.php
'providers' => [
// Other Service Providers
App\Providers\RiskServiceProvider::class,
],
Read also: Laravel 9 Create Multiple Custom Route File Example
Hope it can help you.