创建 Laravel 个存储库并绑定为服务提供商
Creating Laravel repositories and binding as service providers
我有 Symfony 和 Spring 背景,这是我使用 Laravel 的第一个项目,据我所知 Laravel 没有对存储库的内置支持.我找到了几个教程;他们中的一些人试图提供像 Spring 或 Symfony 这样的架构。
例如这个博客 suggests an folder structure 是这样的:
---- Repository
------ Eloquent
-------- UserRepository.php // extends BaseRepository
-------- BaseRepository.php // implements EloquentRepositoryInterface
------ UserRepositoryInterface.php
------ EloquentRepositoryInterface.php
还不错。我感到困惑的一点是,作者建议将这些存储库绑定为服务提供者,并在控制器中作为提供者访问它们。
class RepositoryServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(EloquentRepositoryInterface::class, BaseRepository::class);
$this->app->bind(UserRepositoryInterface::class, UserRepository::class);
}
}
然后我决定在 Github 中找到一个库,它专注于创建一个 Eloquent 存储库,它直接使用控制器中的用户存储库:
class HomeController extends Controller
{
public function index(UserRepository $userRepository)
{
return $userRepository->get();
...
从架构的角度来看,我们是否需要将存储库绑定为提供者? (假设 AWS 或 Elastic Search 可能加入该项目,存储库可能因单个模型而异)
最重要的是,为什么 Laravel 没有内置的存储库模式支持?
谢谢
来自 Laravel Repository Pattern – How to use & why it matters
The last step is to register this service provider in our config/app.php. Open this file and add to providers our provider App\Providers\RepositoryServiceProvider::class
Now our application knows what class it should use when we type an
objects by its interfaces.
这就是为什么您需要像这样绑定接口:
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->bind(EloquentRepositoryInterface::class, BaseRepository::class);
$this->app->bind(UserRepositoryInterface::class, UserRepository::class);
}
为了让 Laravel 知道在控制器中执行的操作时要实例化的内容:
private $userRepository;
public function __construct(UserRepositoryInterface $userRepository)
{
$this->userRepository = $userRepository;
}
[编辑 1]
Laravel 文档中还有一篇关于 依赖注入 的精彩文章:laravel.com/docs/master/controllers#dependency-injection-and-controllers
why doesn't Laravel have a built-in repository pattern
因为对于如果使用它们应该如何使用还没有达成共识。
例如,我使用存储库作为 laravel 模型和 laravel 需要实例化模型实例的控制器之间的中介,我从不将它们注入控制器,而是在需要时手动实例化它们。
do we need to bind repositories as providers ?
如上所述,没有达成共识,所以NO。
取决于您设计存储库的方式,您可以手动实例化它们,将它们注入控制器的实例化(在 __contruct(UserRepository $userRepository)
中),正如您在 laravel 从头开始教程中看到的 laracast 或使用他们作为服务提供商。
我有 Symfony 和 Spring 背景,这是我使用 Laravel 的第一个项目,据我所知 Laravel 没有对存储库的内置支持.我找到了几个教程;他们中的一些人试图提供像 Spring 或 Symfony 这样的架构。
例如这个博客 suggests an folder structure 是这样的:
---- Repository
------ Eloquent
-------- UserRepository.php // extends BaseRepository
-------- BaseRepository.php // implements EloquentRepositoryInterface
------ UserRepositoryInterface.php
------ EloquentRepositoryInterface.php
还不错。我感到困惑的一点是,作者建议将这些存储库绑定为服务提供者,并在控制器中作为提供者访问它们。
class RepositoryServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(EloquentRepositoryInterface::class, BaseRepository::class);
$this->app->bind(UserRepositoryInterface::class, UserRepository::class);
}
}
然后我决定在 Github 中找到一个库,它专注于创建一个 Eloquent 存储库,它直接使用控制器中的用户存储库:
class HomeController extends Controller
{
public function index(UserRepository $userRepository)
{
return $userRepository->get();
...
从架构的角度来看,我们是否需要将存储库绑定为提供者? (假设 AWS 或 Elastic Search 可能加入该项目,存储库可能因单个模型而异)
最重要的是,为什么 Laravel 没有内置的存储库模式支持? 谢谢
来自 Laravel Repository Pattern – How to use & why it matters
The last step is to register this service provider in our config/app.php. Open this file and add to providers our provider App\Providers\RepositoryServiceProvider::class
Now our application knows what class it should use when we type an objects by its interfaces.
这就是为什么您需要像这样绑定接口:
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->bind(EloquentRepositoryInterface::class, BaseRepository::class);
$this->app->bind(UserRepositoryInterface::class, UserRepository::class);
}
为了让 Laravel 知道在控制器中执行的操作时要实例化的内容:
private $userRepository;
public function __construct(UserRepositoryInterface $userRepository)
{
$this->userRepository = $userRepository;
}
[编辑 1]
Laravel 文档中还有一篇关于 依赖注入 的精彩文章:laravel.com/docs/master/controllers#dependency-injection-and-controllers
why doesn't Laravel have a built-in repository pattern
因为对于如果使用它们应该如何使用还没有达成共识。
例如,我使用存储库作为 laravel 模型和 laravel 需要实例化模型实例的控制器之间的中介,我从不将它们注入控制器,而是在需要时手动实例化它们。
do we need to bind repositories as providers ?
如上所述,没有达成共识,所以NO。
取决于您设计存储库的方式,您可以手动实例化它们,将它们注入控制器的实例化(在 __contruct(UserRepository $userRepository)
中),正如您在 laravel 从头开始教程中看到的 laracast 或使用他们作为服务提供商。