fanmade / laravel-service-binding
提供配置,允许服务和存储库绑定以及通过环境变量在它们之间切换
0.5.0
2020-10-04 20:38 UTC
Requires
- php: >=7.2
- illuminate/support: ^6 || ^7 || ^8
This package is auto-updated.
Last update: 2024-08-28 23:06:56 UTC
README
早期开发中,不建议使用
Laravel 服务绑定
Laravel 提供了所有必要的工具,允许使用服务或存储库绑定。只需在服务提供者中绑定即可。
public function register()
{
$this->app->bind(FooRepositoryInterface::class, EloquentFooRepository::class);
$this->app->bind(FooSearchServiceInterface::class, EloquentFooSearchService::class);
}
现在,如果你创建了一个 ElasticSearchFooSearchService
并正确完成了所有操作,你只需更改绑定,一切都应该正常工作。但是,你可能出于多种原因在不同的系统上使用不同的服务,这会很快变得复杂。你也不能在不修改代码的情况下在不同的存储库之间切换 :(: 可能你现在开始在服务提供者中使用 if
/else
。
public function register()
{
$this->app->bind(FooRepositoryInterface::class, EloquentFooRepository::class);
if (env('ELASTICSEARCH_FOO_SERVICE', 'default')) {
$this->app->bind(FooSearchServiceInterface::class, ElasticSearchFooSearchService::class);
} else {
$this->app->bind(FooSearchServiceInterface::class, EloquentFooSearchService::class);
}
}
或者你切换到 Symphony,那里没有这个问题,因为你只需更新你的配置文件。
这个包尝试为 Laravel 应用程序提供一个解决方案。