我正在嘗試學習存儲庫模式,似乎讓我自己有點混淆,因爲我急於加載關係時如何使用這個存儲庫模式,並將數據庫邏輯保留在我的控制器之外。Laravel - 使用存儲庫模式
我的存儲庫/應用程序結構的快速概覽。
app/
Acme/
Repositories/
RepositoryServiceProvider.php
Product/
EloquentProduct.php
ProductInterface.php
Category/
EloquentCategory.php
CategoryInterface.php
例ProductInterface.php
<?php namespace GD\Repositories\Product;
interface ProductInterface
{
public function all();
public function find($id);
public function findBySlug($slug);
}
例CategoryInterface.php
<?php namespace GD\Repositories\Category;
interface CategoryInterface
{
public function all();
public function find($id);
public function findBySlug($slug);
}
好了,所以比較容易的部分是利用DI噴射模型的依賴關係到控制器中。
列出所有類別與相關產品更困難,因爲我不再使用雄辯模型。我正在使用一個沒有公開所有雄辯方法的界面。
這不會沒有我實現我的EloquentCategory類中方法工作...
public function show($slug)
{
return Response::json($this->category->findBySlug($slug)->with('products'), 200);
}
我應該創建一個單獨的服務類兩個儲存粘合在一起?例如允許以下內容
public function __construct(ShopService $shop)
{
$this->shop = $shop;
}
public function show($slug)
{
return Response::json($this->shop->getProductsInCategory($slug), 200);
}
或者,我是否應該在我的Category Repository中實現with方法?
public function with($relation)
{
return Category::with($relation);
}
最後,我是否理解了Repository Pattern的用法正確?
app/Acme/etc ...有人來自laracasts:P –