How to Use Laravel Service Provider
Updated Jan 11, 2021 View by 4.2 K

In this article, we’re going to discuss How to Use Laravel Service Provider in detail. In the course of this article, I’ll also demonstrate how to create a custom service provider in Laravel. Once you create a service provider, you also need to register it with the Laravel application in order to actually use it, so we’ll go through that as well.
How to Use Laravel Service Provider
There are two important methods, boot, and register, that your service provider may implement, and in the last segment of this article, we’ll discuss these two methods thoroughly.
What Is a Service Provider?
If the service container is something that allows you to define bindings and inject dependencies, then the service provider is the place where it happens.
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.
But, what do we mean by “bootstrapped”? In general, we mean registering things, including registering service container bindings, event listeners, middleware, and even routes. Service providers are the central place to configure your application.
Let’s have a quick look at one of the core service providers to understand what it does. Go ahead and open the vender/laravel/framework/src/Illuminate/Cache/CacheServiceProvider.php
file.
public function register() { $this->app->singleton('cache', function ($app) { return new CacheManager($app); }); $this->app->singleton('cache.store', function ($app) { return $app['cache']->driver(); }); $this->app->singleton('memcached.connector', function () { return new MemcachedConnector; }); }
The important thing to note here is the register
method, which allows you to define service container bindings. As you can see, there are three bindings for the cache
, cache.store
and memcached.connector
services.
Create Your Service Provider
$php artisan make:provider ProgrammingPotServiceProvider Provider created successfully.
And that should create the file ProgrammingPotServiceProvider.php
under the app/Providers
directory. Open the file to see what it holds.
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; class EProgrammingPotServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { // } /** * Register the application services. * * @return void */ public function register() { // } }
Register Service Provider
To register your service provider, you just need to add an entry to the array of service providers in the config/app.php
file.
'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... */ Laravel\Tinker\TinkerServiceProvider::class, /* * 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\ProgrammingPotServiceProvider::class, ],
And that’s it! You’ve registered your service provider with Laravel’s scheme of things! But the service provider we’ve created is almost a blank template and of no use at the moment. In the next section, we’ll go through a couple of practical examples to see what you could do with the register and boot methods.
Go Through the Register and Boot Methods
So, what if we need to register a view composer within our service provider? This should be donewithin the boot
method. This method is called after all other service providers have been registered, meaning you have access to all other services that have been registered by the framework:
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; class ProgrammingPotProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { view()->composer('view', function () { // }); } }