
In last guide we learned about Service Container in Laravel and i told you about that we will make a new guide about Service Provider in Laravel.
In Older version of laravel there was no Service Provider and people was keep asking where we should put the service to the service container and where should we bind them. Then Laravel launch this feature to clear all confusion.
Service Provider in Laravel
Service providers are the central place of all Laravel application bootstrapping. Your own application, as well as all of Laravel’s core services, are bootstrapped via service providers.
By Laravel
As per Laravel its a center point where all laravel and your own services Power Ups. did i said Power Up? Yes because its automatic process to register service container bindings, events listeners, middleware and routes etc.
Let’s take a look at config/app.php
file there is a providers array which have all service provider classes that will load for your laravel application.
A fresh laravel app has a set of Laravel core service provider that is listed in providers array.
These are Laravel Components such as Mailer, Cookies, Cache, Database, Session and many more.
Some Provider are deferred provider that not load on every request and load only when needed.
Why We Need a Service Provider?
As we see in last guide of Service Container there we use a Service successfully without any service provider so why we need this one. The Answer is very simple when we have one or more required parameter and dependency in our service then we need to provide the dependency to that particular service we need to instantiate into the service class so that time we need a service provider.
Creating a Service Provider
Service Provider is a Class that extends Illuminate\Support\ServiceProvider
class. A service provider contain at least two method when we make by artisan command one is register()
and second is boot()
.
Artisan Command to Make a Service Provider
$ php artisan make:provider MyTestServiceProvider
After run this command you will get a new provider in your app/Providers folder of your Laravel App.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class MyTestServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}
This is simple Provider Class that you get after run the command.
Register Method in Service Provider
In register method you should only bind your Service to Service Container. Keep in mind that you never add your event listener, routes and other functionality to the register method. If you not follow the rule then you will face issue to using a service that is not loaded yet.
In the service provider you can access your entire app via $app
. This $app comes form Illuminate\Support\ServiceProvider
and its take a parameter __construct()
from \Illuminate\Contracts\Foundation\Application
instance.
We will Learn More more property like bindings And singletons Properties and also bind and singleton method.
Boot Method in Service Provider
Its is a method that called after the all services registered by the Laravel. For example if we want to share some data to all available views so then we can use this method.
<?php
namespace App\Providers;
use App\Category;
use App\Post;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class MyTestServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
// categories globally share to all view -- just for testing not include any Model of Category and Post
View::share('categories', Category::all());
// latest post share to all view
$latestPost = Post::latest()->take(3)->get();
View::share('latestPosts', $latestPost);
}
}
You can also use Dependency Injection in boot method and service container automatically inject any dependencies one or more that you need.
Registering a Service Provider
As we discussed above that all service provider registered into config/app.php
file’s providers array. Its very simple to register just put your custom service provider into providers array as the other provider is registered.
'providers' => [
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
\App\Providers\MyTestServiceProvider::class,
],
Deferred Providers
We checked that how to make and use a simple service provider now if we need a deferred service means load only when we need then you should implement a \Illuminate\Contracts\Support\DeferrableProvider
interface and add provides method and return a array of service container bindings.
<?php
namespace App\Providers;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
class MyTestServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
public function provides(): array
{
return [];
}
}
Conclusion
That’s All for now. We learned a lot today and also clear why we use service provider in laravel. If you still have confusion about service provider leave a comment and we will modify some of line that can make you easy to remember and understand. So let meet in new guide 😊.