sajad torkamani

In a nutshell

Service providers bootstrap all of Laravel’s core services (e.g., routing, validation, mailer, database, queues, etc). You also use service providers to bootstrap your own app-specific services.

By “bootstrap” we mean registering things like:

  • Service container bindings
  • Event listeners
  • Middleware
  • Routes

Write a service provider

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.

You can generate a provider using:

php artisan make:provider MyProvider

This will register your provider in your app’s bootstrap/providers.php file.

The register method

Here you’ll bind instances to the service container. For example:

<?php
 
namespace App\Providers;
 
use App\Services\Riak\Connection;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider;
 
class RiakServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        $this->app->singleton(Connection::class, function (Application $app) {
            return new Connection(config('riak'));
        });
    }
}

The boot method

By the time the boot method is called, all services registered by the framework, third-party code or other user-defined service providers will be available.

<?php
 
namespace App\Providers;
 
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
 
class ComposerServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        View::composer('view', function () {
            // ...
        });
    }
}

Register providers

If you use the php artisan make:provider command, Laravel will automatically add the generated provider to the bootstrap/providers.php file. However, if you have manually created the provider class, you should manually add the provider class to the array:

<?php
 
return [
    App\Providers\AppServiceProvider::class,
];

To improve performance, you can also defer the registration of a provider if it’s only registering bindings in the service container:

<?php
 
namespace App\Providers;
 
use App\Services\Riak\Connection;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
 
class RiakServiceProvider extends ServiceProvider implements DeferrableProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        $this->app->singleton(Connection::class, function (Application $app) {
            return new Connection($app['config']['riak']);
        });
    }
 
    /**
     * Get the services provided by the provider.
     *
     * @return array<int, string>
     */
    public function provides(): array
    {
        return [Connection::class];
    }
}

That way, Laravel will only load the service provider when you attempt to resolve one of the registered services.

Sources/further reading

Tagged: Laravel