Laravel服务提供者

2025-02-27 31

一、Laravel服务提供者介绍

服务提供者是所有 Laravel 应用的引导中心。你的应用,以及所有 Laravel 核心服务,都是通过服务提供者引导的。

但是,「引导」是什么意思呢?通常,我们可以理解为注册,比如注册服务容器绑定,事件监听器,中间件,甚至是路由。服务提供者是配置应用程序的中心。

Laravel 在其内部使用多个服务提供者引导其核心服务,比如邮件、队列、缓存等。许多服务提供者是延迟加载的,也就是说他们不会在每次请求时都加载,只有他们的服务实际被需要时才会加载。

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

二、编写服务提供者

所有的服务提供者都继承了 Illuminate\Support\ServiceProvider 类。大部分服务提供者都包含一个 register 方法和一个 boot 方法。 在 register 方法中,你只应将服务绑定到服务容器。而不要尝试在 register 方法中注册任何监听器,路由,或者其他任何功能。

使用 Artisan 命令行工具的 make:provider 命令可以生成新的服务提供者类。Laravel 将会在应用的 bootstrap/providers.php 文件中自动注册你新增的服务提供者:

php artisan make:provider RiakServiceProvider

1、Register 方法

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

让我们来看一个基础的服务提供者。在任何服务提供者方法中,你总是通过 $app 属性来访问服务容器:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?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'));
});
}
}
<?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')); }); } }
<?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'));
});
}
}

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

bindings 和 singletons 属性

如果你的服务提供者注册了许多简单的绑定,你可能想用 bindings 和 singletons 属性而不是手动注册每个容器绑定。当框架加载服务提供者时,它会自动检测这些属性并注册其绑定:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?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,
];
}
<?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, ]; }
<?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,
];
}

2、启动方法

如果我们需要在服务提供者中注册视图组合器,应该在 boot 方法中完成。这个方法在所有其他服务提供者注册之后调用,这意味着你可以访问框架注册的所有其他服务:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider
{
/**
* 启动任何应用服务。
*/
public function boot(): void
{
View::composer('view', function () {
// ...
});
}
}
<?php namespace App\Providers; use Illuminate\Support\Facades\View; use Illuminate\Support\ServiceProvider; class ComposerServiceProvider extends ServiceProvider { /** * 启动任何应用服务。 */ public function boot(): void { View::composer('view', function () { // ... }); } }
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider
{
/**
* 启动任何应用服务。
*/
public function boot(): void
{
View::composer('view', function () {
// ...
});
}
}

3、启动方法的依赖注入

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
use Illuminate\Contracts\Routing\ResponseFactory;
/**
* 启动任何应用服务。
*/
public function boot(ResponseFactory $response): void
{
$response->macro('serialized', function (mixed $value) {
// ...
});
}
use Illuminate\Contracts\Routing\ResponseFactory; /** * 启动任何应用服务。 */ public function boot(ResponseFactory $response): void { $response->macro('serialized', function (mixed $value) { // ... }); }
use Illuminate\Contracts\Routing\ResponseFactory;
/**
* 启动任何应用服务。
*/
public function boot(ResponseFactory $response): void
{
$response->macro('serialized', function (mixed $value) {
// ...
});
}

三、注册服务提供者

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
return [
App\Providers\AppServiceProvider::class,
];
<?php return [ App\Providers\AppServiceProvider::class, ];
<?php
return [
App\Providers\AppServiceProvider::class,
];

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
return [
App\Providers\AppServiceProvider::class,
App\Providers\ComposerServiceProvider::class, // [tl! add]
];
<?php return [ App\Providers\AppServiceProvider::class, App\Providers\ComposerServiceProvider::class, // [tl! add] ];
<?php
return [
App\Providers\AppServiceProvider::class,
App\Providers\ComposerServiceProvider::class, // [tl! add]
];

四、延迟提供者

如果你的提供者仅在服务容器中注册绑定,你可以选择延迟其注册,直到实际需要注册的绑定之一。延迟加载这样的提供者将提高你的应用性能,因为它不会在每个请求上从文件系统加载。

Laravel 编译并存储由延时服务提供者提供的所有服务的列表,以及其服务提供商类的名称。然后,只有当你尝试解析其中一个服务时,Laravel 才会加载服务提供商。

要延时加载提供商,请实现 \Illuminate\Contracts\Support\DeferrableProvider 接口并定义 provides 方法。provides 方法应返回由提供商注册的服务容器绑定:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?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
{
/**
* 注册任何应用服务
*/
public function register(): void
{
$this->app->singleton(Connection::class, function (Application $app) {
return new Connection($app['config']['riak']);
});
}
/**
* 获取服务提供者
*
* @return array<int, string>
*/
public function provides(): array
{
return [Connection::class];
}
}
<?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 { /** * 注册任何应用服务 */ public function register(): void { $this->app->singleton(Connection::class, function (Application $app) { return new Connection($app['config']['riak']); }); } /** * 获取服务提供者 * * @return array<int, string> */ public function provides(): array { return [Connection::class]; } }
<?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
{
/**
* 注册任何应用服务
*/
public function register(): void
{
$this->app->singleton(Connection::class, function (Application $app) {
return new Connection($app['config']['riak']);
});
}
/**
* 获取服务提供者
*
* @return array<int, string>
*/
public function provides(): array
{
return [Connection::class];
}
}

————————————————
原文作者:Laravel China 社区文档:《Laravel 11 中文文档(11.x)》
转自链接:https://learnku.com/docs/laravel/11.x/providersmd/16655

  • 广告合作

  • QQ群号:707632017

温馨提示:
1、本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。邮箱:2942802716#qq.com(#改为@)。 2、本站原创内容未经允许不得转裁,转载请注明出处“站长百科”和原文地址。
DeepSeek
上一篇: DeepSeek写SQL
Laravel服务提供者
下一篇: Laravel Facades