Laravel-使用存储库模式 [英] Laravel - Using the Repository Pattern

查看:62
本文介绍了Laravel-使用存储库模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习存储库模式,似乎让我有些困惑,当我渴望加载关系并将db逻辑保留在控制器之外时,如何使用该存储库模式.

I am trying to learn the repository pattern, and seem to have got myself a tad confused with how I can use this repository pattern when eager loading relationships and keep db logic out of my controller.

我的存储库/应用程序结构的快速概述.

A quick overview of my repository / application structure.

app/
  Acme/
    Repositories/
      RepositoryServiceProvider.php
      Product/
        EloquentProduct.php
        ProductInterface.php
      Category/
        EloquentCategory.php
        CategoryInterface.php

ProductInterface.php示例

Example ProductInterface.php

<?php namespace GD\Repositories\Product;

interface ProductInterface
{
    public function all();

    public function find($id);

    public function findBySlug($slug);
}

示例CategoryInterface.php

Example CategoryInterface.php

<?php namespace GD\Repositories\Category;

interface CategoryInterface
{
    public function all();

    public function find($id);

    public function findBySlug($slug);
}

好,所以最简单的部分是使用DI将模型依赖项注入到控制器中.

Ok, so the easy part is using DI to inject model dependencies into the controller.

列出具有关联产品的所有类别比较困难,因为我不再使用雄辩的模型了.我正在使用尚未公开所有雄辩方法的接口.

listing all categories with associated products is more difficult as I am no longer working with an eloquent model. I am working with an interface which has not exposed all the eloquent methods.

如果没有在我的EloquentCategory类中实现 with 方法,这将无法工作...

This won't work without me implementing a with method within my EloquentCategory class...

public function show($slug)
{
  return Response::json($this->category->findBySlug($slug)->with('products'), 200);
}

我应该创建一个单独的服务类来将两个存储库粘合在一起吗?例如允许以下

Should I create a separate service class to glue the two repositories together? e.g. allowing the following

public function __construct(ShopService $shop)
{
  $this->shop = $shop;
}

public function show($slug)
{
  return Response::json( $this->shop->getProductsInCategory($slug), 200 );
}

或者,是否应该在类别存储库中实现with方法?

Or alternatively, should I be implementing the with method in my Category Repository?

public function with($relation)
{
  return Category::with($relation);
}

最后,我对存储库模式用法的理解正确吗?

Finally, is my understanding of the usage of the Repository Pattern Correct?

推荐答案

您正在考虑,存储库只是您的controllermodel之间的链接/桥,因此控制器使用repository类而不是model直接在该存储库中,您可以从此处使用model声明您的方法,例如:

You are over thinking, repository is just a link/bridge between your controller and model and hence controller uses repository class instead of the model directly and in that repository you may declare your methods using the model from there, for example:

<?php namespace GD\Repositories\Category;

interface CategoryInterFace{

    public function all();

    public function getCategoriesWith($with);

    public function find($id);
}

现在在存储库类中实现接口:

Now implement the interface in the repository class:

<?php namespace GD\Repositories\Category;

use \EloquentCategory as Cat; // the model
class CategoryRepository implements CategoryInterFace {
    public function all()
    {
        return Cat::all();
    }

    public function getCategoriesWith($with)
    {
        return Cat::with($with)->get();
    }

    public function find($id)
    {
        return Cat::find($id):
    }
}

要在控制器中使用它,

<?php

use GD\Repositories\Category\CategoryInterFace;

class CategoryController extends BaseController {

    public function __construct(CategoryInterFace $category)
    {
        $this->cat = $category;
    }

    public function getCatWith()
    {
        $catsProd = $this->cat->getCategoriesWith('products');
        return $catsProd;
    }

    // use any method from your category
    public function getAll()
    {
        $categories = $this->cat->all();

        return View::make('category.index', compact('categories'));
    }

}

注意:因为这不是您的问题,所以省略了存储库的IoC绑定.

Note: Omitted the IoC binding of the repository because this is not your problem and you know that.

更新:我在这里写了一篇文章: LARAVEL –使用存储库模式.

Update: I've written an article here: LARAVEL – USING REPOSITORY PATTERN.

这篇关于Laravel-使用存储库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆