Service Provider


brief introduction

The service provider is the center for launching Larravel applications. Your own applications and all Larravel core services are launched through the service provider.

But what do we mean by "startup"? Usually, this means registering services, including registering service container bindings, event listeners, middleware, and even routing. The service provider is the center of application configuration.

If you open Laravel's config/app.php File, you will see a providers Array, here are all the service provider classes that the application needs to load. Of course, many of them are loaded late, that is, not every request will be loaded, but only when they are really used.

Through this document, you will learn how to write your own service providers and register them in the Larravel application.

Write Service Provider

All service providers inherit from Illuminate\Support\ServiceProvider Class. Most service providers include two methods: register and boot stay register In the method, the only thing you need to do is bind the service to Service Container , do not try to register event listeners, routing or any other functions in this method.

Through Artisan commands make:provider A new provider can be generated:

 php artisan make:provider RiakServiceProvider

Register method

As mentioned earlier, in the register Only binding service to Service Container Do not do anything else. Otherwise, you may accidentally use the service provided by a service provider that has not been loaded.

Now let's see what a basic service provider looks like:

 <? php namespace App\Providers; use Riak\Connection; use Illuminate\Support\ServiceProvider; class RiakServiceProvider extends ServiceProvider{ /** *Register the binding in the container * * @return void */ public function register() { $this->app->singleton(Connection::class, function ($app) { return new Connection(config('riak')); }); } }

The service provider only defines one register Method, and use this method to define a Riak\Connection Implementation of. If you do not know how the service container works, please refer to Its documentation

Boot method

What should we do if we want to register the view composer in the service provider? This needs boot Method. This method will not be called until all service providers are registered, which means that we can access all other services registered by the framework:
 <? php namespace App\Providers; use Illuminate\Support\ServiceProvider; class ComposerServiceProvider extends ServiceProvider{ /** * Perform post-registration booting of services. * * @return void */ public function boot() { view()->composer('view', function () { // }); } }

Dependency injection of boot method

We can boot Type prompt for dependency in the method, Service Container It will automatically inject the dependencies you need:

 use Illuminate\Contracts\Routing\ResponseFactory; public function boot(ResponseFactory $response){ $response->macro('caps', function ($value) { // }); }

Registered Service Provider

All service providers use configuration files config/app.php This file contains a list of all service provider names providers Array, which lists all core service providers by default, and these service providers start the core components of Laravel, such as mail, queue, cache, and so on.

To register your own service provider, just add it to the array:

 'providers' => [ //Other service providers App\Providers\ComposerServiceProvider::class, ],

Delay loading service providers

If your provider is just Service Container To register the binding in, you can choose to delay loading the binding until the service registering the binding really needs it. Delaying loading such a provider will improve the performance of the application, because it will not be loaded from the file system every time it is requested.

Larravel compiles and saves the service provided by all delay service providers and their class names. Then, Larvel will load its service provider only when you try to parse one of the services.

To delay loading a provider, set defer Property is true And define a provides Method, which returns the service container binding registered by the provider:

 <? php namespace App\Providers; use Riak\Connection; use Illuminate\Support\ServiceProvider; class RiakServiceProvider extends ServiceProvider{ /** *Service provider adds whether to delay loading * * @var bool */ protected $defer = true; /** *Registered Service Provider * * @return void */ public function register() { $this->app->singleton(Connection::class, function ($app) { return new Connection($app['config']['riak']); }); } /** *Get the service provided by the provider * * @return array */ public function provides() { return [Connection::class]; } }
Note: It is highly recommended to read Deep understanding of inversion of control (IoC) and dependency injection (DI) Deeply understand the implementation principles of service containers and service providers.

give the thumbs-up Cancel Like Collection Cancel Collection

<<Previous: Service Container

>>Next: Facades