pp-spaces/laravel-repository

Laravel 的仓储设计模式实现

v1.0.1 2019-12-27 04:27 UTC

This package is auto-updated.

Last update: 2024-09-27 15:25:12 UTC


README

Latest Stable Version Total Downloads Build Status License

内容

升级通知

注意:我修改了创建 Repository 的方式,因为我希望 Repository 使用 Route Model Binding 以加快数据查询。

什么是仓储设计模式?

Repository Design Pattern

简单来说,它是在应用程序和数据源之间实现一个代理层的实现。任何一方都不需要知道另一方来执行各自的任务,这使得我们可以有一个解耦的架构,从而有助于在大规模应用程序中扩展,而不需要硬依赖。

这是万能钥匙吗?

嗯,不是。像每个设计模式一样,它有其优点和缺点。

优点

  • 关注点分离;应用程序不需要知道或跟踪任何或所有数据源。
  • 允许轻松进行单元测试,因为仓储绑定到接口,这些接口在运行时注入到类中。
  • DRY(不要重复自己)设计,查询和从数据源检索数据的代码不会重复。

缺点

  • 增加了一个抽象层,这增加了复杂度,使得它对于小型应用程序来说有些过度。

源代码

用法

此包为您提供了命令行界面,用于在您的 Laravel 应用程序中创建仓库。

安装

pp-spaces/laravel-repository 包添加到您的 Laravel 安装中

composer require pp-spaces/laravel-repository

创建仓库

运行以下命令以生成仓库

php artisan make:repository UserRepository

要创建模型仓库,只需运行

php artisan make:repository UserRepository --model=User

用例

   +-------------+                                +-------------+       +-------------+
   |             |                                |             |       |             |
   |             |---------------------------------             ---------             |
   |             |      +-------------------+     |             |       |             |
   | Controllers |      |      Persist      |     | Repository  |       |   Models    |
   |             |      |   Database Query  |     |             |       |             |
   |             |      +-------------------+     |             |       |             |
   |             ---------------------------------|             ---------             |
   |             |                                |             |       |             |
   +-------------+                                +-------------+       +-------------+

如何使用仓库

创建您的仓库,例如 UserRepository 用于 User 模型

php artisan make:repository UserRepository --model=User

更新 UserRepository 逻辑

namespace App\Http\Repositories;

use PPSpaces\Repositories\Repository;

class UserRepository extends Repository {

    /**
     * The user model instance.
     *
     * @var \App\User
     */
    protected $model = "App\User";

    /**
     * Scope a query for the model before executing
     *
     * @param \Illuminate\Database\Query\Builder $query
     * @return void
     */
    public function before($query) {
        $query->role('staff');
    }

    /**
     * Get all of the models from the database.
     *
     * @param  array|mixed  $columns
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function get($columns = ['*']) {
        $users = $this->repository
                    ->active()
                    ->orderBy('updated_at', 'DESC')
                    ->get();

        return $users;
    }
}

注意:检查 PPSpaces\Repositories\Model 以获取您可能需要重写的可用方法。请注意,您仍然可以访问您创建的所有模型实例。$this->user 是您的 \App\User 模型实例。

在您的 UserController 中,假设您已创建了一个资源控制器。将 UserRepository 注入到控制器中。现在您可以在控制器方法中访问仓库

use App\Http\Repositories\UserRepository;

class UserController extends Controller
{
    protected $users;

    public function __construct(UserRepository $users)
    {
        $this->users = $users;
    }

    public function index()
    {
        return $this->users->get();
    }
}

或者,您还可以在控制器操作中使用 路由模型绑定,其 type-hinted 变量名称与路由段名称匹配。

有关 路由模型绑定 的更多信息,请在此处查看

public function index(UserRepository $user)
{
    return $user->get();
}

public function show(UserRepository $user)
{
    // Authorizing the repository model
    // Check https://laravel.net.cn/docs/master/authorization
    $this->authorize('view', $user->model());

    // This $user will resolved by the id provided by the router
    // e.g. /api/user/1
    // $user will be the result of $user->id === 1
    return $user;
}

如何升级?

v0.0.9 或更早版本升级到 v1.0.0

您需要做什么

namespace App\Http\Repositories;

- use App\User;

- use PPSpaces\Repositories\Model as Repository;
+ use PPSpaces\Repositories\Repository;

class UserRepository extends Repository {

+    /**
+     * The user model instance.
+     *
+     * @var \App\User
+     */
+    protected $model = "App\User";

-     protected $user;

-    public function __construct(User $user) {
-        $this->user = $user;
-    }

    public function index()
    {
         // `$this->users->all()` will always resolved the same result as `$this->users->get()`
-        return $this->users->all();
+        return $this->users->get();
    }

}

帮助

Description:
  Create a new repository class

Usage:
  make:repository [options] [--] <name>

Arguments:
  name                  The name of the class

Options:
  -m, --model[=MODEL]   Generate a repository for the given model.
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
      --env[=ENV]       The environment the command should run under
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug