使用 $this->app->singleton() 可在 Laravel 服务提供者中注册单例,确保每次解析都返回同一实例。在register() 方法中绑定类或接口,如 singleton(PaymentGateway::class, function() {...}) 或将接口绑定到实现,如 singleton('App\Contracts\PaymentInterface', 'App\Services\StripePaymentService'),推荐接口方式以解耦。单例在请求内唯一,应避免在 boot() 中注册,无自定义逻辑时可直接传类名。这是 Laravel 管理全局唯一服务的标准做法。
在 Laravel 的服务提供者中注册单例,可以通过 $this->app->singleton() 方法实现。这个方法确保容器每次解析该服务时,都返回同一个实例。
在自定义服务提供者的 register() 方法中,调用 singleton() 来绑定一个类或接口到容器,代码如下:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\PaymentGateway;
class PaymentServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(PaymentGateway::class, function ($app) {
return new PaymentGateway('your-api-key');
});
}
}
这样,无论在哪里通过依赖注入获取 PaymentGateway,Laravel 都会返回同一个实例。
更常见的做法是将接口绑定到具体的单例实现,便于解耦和测试:
$this->app->singleton(
'App\Contracts\PaymentInterface',
'App\Services\StripePaymentService'
);
或者使用闭包:
$this->app->singleton('App\Contracts\LoggerInterface', function ($app) {
return new FileLogger(storage_path('logs/app.log'));
});