服务提供者是所有 Laravel 应用程序引导的核心位置。 你自己的应用程序,以及所有 Laravel 的核心服务,都通过服务提供者进行引导。
但是,我们所说的"引导"是什么意思?通常,我们指的是注册各种事物,包括注册服务容器绑定,事件监听器,中间件,甚至路由。服务提供者是配置应用程序的中心位置。
Laravel 在内部使用数十个服务提供者来引导其核心服务,例如邮件服务、队列、缓存等。其中许多服务提供者是“延迟”提供者,这意味着它们不会在每个请求上加载,而只在实际需要它们所提供的服务时才加载。
所有用户定义的 服务提供者 都注册在 bootstrap/providers.php 文件中。在以下文档中,您将学习如何编写自己的服务提供者,并将其注册到您的 Laravel 应用程序中。
[!注意]
如果您想了解更多关于 Laravel 如何处理请求并在内部工作的信息,请查阅我们关于 Laravel 请求生命周期的文档。
所有服务提供者扩展 Illuminate\Support\ServiceProvider 类。大多数服务提供者包含 register 和 boot 方法。在 register 方法中,你应该 只将事物绑定到 服务容器。你绝不应尝试在 register 方法中注册任何事件监听器、路由或任何其他功能。
Artisan CLI 可以通过 make:provider 命令生成新的服务提供者. Laravel 将自动将您的新服务提供者注册到您的应用程序的 bootstrap/providers.php 文件中:
php artisan make:provider RiakServiceProvider如前所述,在 register 方法中,你应该只绑定内容到 服务容器中。你绝不应该尝试在 register 方法中注册任何事件监听器、路由或任何其他功能。否则,你可能会意外地使用一个由尚未加载的服务提供者提供的服务。
让我们来看一个基础的服务提供者。
在你的任何服务提供者方法中,你总是可以访问 $app 属性,它提供了对服务容器的访问:
<?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'));
});
}
}这个服务提供者只定义了一个 register 方法,并使用该方法在服务容器中定义了 App\Services\Riak\Connection 的一个实现。如果你还不熟悉 Laravel 的服务容器,请查阅它的文档。
bindings 和 singletons 属性如果您的服务提供者注册了许多简单的绑定,您可能希望使用 bindings 和 singletons 属性,而不是手动注册每个容器绑定。当服务提供者被框架加载时,它将自动检查这些属性并注册它们的绑定:
<?php
namespace App\Providers;
use App\Contracts\DowntimeNotifier;
use App\Contracts\ServerProvider;
use App\Services\DigitalOceanServerProvider;
use App\Services\PingdomDowntimeNotifier;
use App\Services\ServerToolsProvider;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* All of the container bindings that should be registered.
*
* @var array
*/
public $bindings = [
ServerProvider::class => DigitalOceanServerProvider::class,
];
/**
* All of the container singletons that should be registered.
*
* @var array
*/
public $singletons = [
DowntimeNotifier::class => PingdomDowntimeNotifier::class,
ServerProvider::class => ServerToolsProvider::class,
];
}那么,如果我们需要在服务提供者中注册一个视图合成器呢?这应该在boot方法中完成。这个方法在所有其他服务提供者注册完成后被调用,这意味着你可以访问框架注册的所有其他服务:
<?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 () {
// ...
});
}
}你可以为你的服务提供者的 boot 方法进行类型提示. [服务容器](/docs/laravel/12.x/zh-cn/container) 会自动注入你需要的任何依赖:
use Illuminate\Contracts\Routing\ResponseFactory;
/**
* Bootstrap any application services.
*/
public function boot(ResponseFactory $response): void
{
$response->macro('serialized', function (mixed $value) {
// ...
});
}所有服务提供者都在 bootstrap/providers.php 配置文件中注册。此文件返回一个数组,其中包含应用程序服务提供者的类名:
<?php
return [
App\Providers\AppServiceProvider::class,
];当你调用 make:provider Artisan 命令时,Laravel 将自动把生成的提供者添加到 bootstrap/providers.php 文件中。然而,如果你是手动创建的提供者类,你应该手动将该提供者类添加到数组中:
<?php
return [
App\Providers\AppServiceProvider::class,
App\Providers\ComposerServiceProvider::class, // [tl! add]
];如果您的服务提供者仅仅在服务容器中注册绑定,您可以选择推迟其注册,直到其中一个注册的绑定实际被需要时。推迟此类服务提供者的加载将提升您应用程序的性能,因为它不会在每个请求时都从文件系统加载。
Laravel 编译并存储了所有由延迟服务提供者提供的服务列表,以及其服务提供者类的名称。然后,只有当你尝试解析这些服务之一时,Laravel 才会加载该服务提供者。
为了延迟加载提供者,实现 \Illuminate\Contracts\Support\DeferrableProvider 接口并定义一个 provides 方法。该 provides 方法应返回由提供者注册的服务容器绑定:
<?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];
}
}