跳转至内容

服务提供者

简介

服务提供者是所有 Laravel 应用程序引导启动的中心。你自己的应用程序以及所有 Laravel 的核心服务都是通过服务提供者引导启动的。

但是,“引导启动”(bootstrapped)是什么意思呢?通常,我们指的是注册,包括注册服务容器绑定、事件监听器、中间件,甚至是路由。服务提供者是配置应用程序的核心场所。

Laravel 在内部使用了数十个服务提供者来引导其核心服务,例如邮件发送、队列、缓存等。其中许多提供者是“延迟”(deferred)提供者,这意味着它们不会在每个请求中加载,而仅在真正需要它们提供的服务时才会加载。

所有用户定义的服务提供者都在 bootstrap/providers.php 文件中注册。在接下来的文档中,你将学习如何编写自己的服务提供者并将其注册到 Laravel 应用程序中。

如果你想深入了解 Laravel 如何处理请求及其内部工作原理,请查看关于 Laravel 请求生命周期 的文档。

编写服务提供者

所有服务提供者都继承自 Illuminate\Support\ServiceProvider 类。大多数服务提供者包含一个 register 方法和一个 boot 方法。在 register 方法中,你应该只将内容绑定到 服务容器。你绝对不应该在 register 方法中尝试注册任何事件监听器、路由或任何其他功能。

Artisan CLI 可以通过 make:provider 命令生成新的提供者。Laravel 会自动在应用程序的 bootstrap/providers.php 文件中注册你的新提供者。

1php artisan make:provider RiakServiceProvider

Register 方法

如前所述,在 register 方法中,你只能将内容绑定到 服务容器 中。你绝对不应该在 register 方法中尝试注册任何事件监听器、路由或任何其他功能。否则,你可能会意外使用尚未加载的服务提供者所提供的服务。

让我们看一个基本的服务提供者。在任何服务提供者方法中,你始终可以访问 $app 属性,该属性提供了对服务容器的访问权限。

1<?php
2 
3namespace App\Providers;
4 
5use App\Services\Riak\Connection;
6use Illuminate\Contracts\Foundation\Application;
7use Illuminate\Support\ServiceProvider;
8 
9class RiakServiceProvider extends ServiceProvider
10{
11 /**
12 * Register any application services.
13 */
14 public function register(): void
15 {
16 $this->app->singleton(Connection::class, function (Application $app) {
17 return new Connection(config('riak'));
18 });
19 }
20}

此服务提供者仅定义了一个 register 方法,并使用该方法在服务容器中定义了 App\Services\Riak\Connection 的实现。如果你还不熟悉 Laravel 的服务容器,请查阅 相关文档

bindingssingletons 属性

如果你的服务提供者注册了许多简单的绑定,你可能希望使用 bindingssingletons 属性,而不是手动注册每个容器绑定。当框架加载该服务提供者时,它会自动检查这些属性并注册相应的绑定。

1<?php
2 
3namespace App\Providers;
4 
5use App\Contracts\DowntimeNotifier;
6use App\Contracts\ServerProvider;
7use App\Services\DigitalOceanServerProvider;
8use App\Services\PingdomDowntimeNotifier;
9use App\Services\ServerToolsProvider;
10use Illuminate\Support\ServiceProvider;
11 
12class AppServiceProvider extends ServiceProvider
13{
14 /**
15 * All of the container bindings that should be registered.
16 *
17 * @var array
18 */
19 public $bindings = [
20 ServerProvider::class => DigitalOceanServerProvider::class,
21 ];
22 
23 /**
24 * All of the container singletons that should be registered.
25 *
26 * @var array
27 */
28 public $singletons = [
29 DowntimeNotifier::class => PingdomDowntimeNotifier::class,
30 ServerProvider::class => ServerToolsProvider::class,
31 ];
32}

Boot 方法

那么,如果我们需要在服务提供者中注册一个 视图合成器(view composer) 该怎么办?这应该在 boot 方法中完成。此方法在所有其他服务提供者都被注册后调用,这意味着你可以访问框架注册的所有其他服务。

1<?php
2 
3namespace App\Providers;
4 
5use Illuminate\Support\Facades\View;
6use Illuminate\Support\ServiceProvider;
7 
8class ComposerServiceProvider extends ServiceProvider
9{
10 /**
11 * Bootstrap any application services.
12 */
13 public function boot(): void
14 {
15 View::composer('view', function () {
16 // ...
17 });
18 }
19}

Boot 方法依赖注入

你可以为服务提供者的 boot 方法进行依赖注入类型提示。 服务容器 会自动注入你需要的任何依赖项。

1use Illuminate\Contracts\Routing\ResponseFactory;
2 
3/**
4 * Bootstrap any application services.
5 */
6public function boot(ResponseFactory $response): void
7{
8 $response->macro('serialized', function (mixed $value) {
9 // ...
10 });
11}

注册提供者

所有服务提供者都在 bootstrap/providers.php 配置文件中注册。该文件返回一个数组,其中包含应用程序服务提供者的类名。

1<?php
2 
3return [
4 App\Providers\AppServiceProvider::class,
5];

当你调用 make:provider Artisan 命令时,Laravel 会自动将生成的提供者添加到 bootstrap/providers.php 文件中。但是,如果你手动创建了提供者类,则应该手动将该提供者类添加到数组中。

1<?php
2 
3return [
4 App\Providers\AppServiceProvider::class,
5 App\Providers\ComposerServiceProvider::class,
6];

延迟提供者

如果你的提供者服务容器 中注册绑定,则可以选择延迟其注册,直到真正需要其中一个注册的绑定时再进行加载。延迟加载此类提供者将提高应用程序的性能,因为它不会在每个请求中都从文件系统加载。

Laravel 会编译并存储一份由延迟服务提供者提供的所有服务的列表,以及对应的服务提供者类名。然后,仅当你尝试解析这些服务之一时,Laravel 才会加载该服务提供者。

要延迟提供者的加载,请实现 \Illuminate\Contracts\Support\DeferrableProvider 接口并定义一个 provides 方法。provides 方法应返回该提供者所注册的服务容器绑定。

1<?php
2 
3namespace App\Providers;
4 
5use App\Services\Riak\Connection;
6use Illuminate\Contracts\Foundation\Application;
7use Illuminate\Contracts\Support\DeferrableProvider;
8use Illuminate\Support\ServiceProvider;
9 
10class RiakServiceProvider extends ServiceProvider implements DeferrableProvider
11{
12 /**
13 * Register any application services.
14 */
15 public function register(): void
16 {
17 $this->app->singleton(Connection::class, function (Application $app) {
18 return new Connection($app['config']['riak']);
19 });
20 }
21 
22 /**
23 * Get the services provided by the provider.
24 *
25 * @return array<int, string>
26 */
27 public function provides(): array
28 {
29 return [Connection::class];
30 }
31}