Repositories 本文未发布 发布文章

未匹配的标注

Definition

The Repository classes are an implementation of the Repository Design Pattern.
Their major roles are separating the business logic from the data (or the data access Task).
Repositories saves and retrieves Models to/from the underlying storage mechanism.
The Repository is used to separate the logic that retrieves the data and maps it to a Model, from the business logic that acts on the Model.

Principles

  • Every Model SHOULD have a Repository.
  • A Model SHOULD always get accessed through its Repository. (Never direct access to Model).

Rules

  • All Repositories MUST extend from App\Ship\Parents\Repositories\Repository. Extending from this class will give access to functions like (find, create, update and much more).
  • Repository name should be same like it’s model name (model: Foo -> repository: FooRepository).
  • If a Repository belongs to a Model whose name is not equal to its Container name, then the Repository must set the $container property like this: $container='ContainerName'. See an example below

Folder Structure

 - app
    - Containers
        - {container-name}
                - Data
                - Repositories
                    - UserRepository.php
                    - ...

Code Samples

User Repository:

<?php

namespace App\Containers\User\Data\Repositories;

use App\Containers\User\Contracts\UserRepositoryInterface;
use App\Containers\User\Models\User;
use App\Ship\Parents\Repositories\Repository;

class UserRepository extends Repository implements UserRepositoryInterface
{
    protected $fieldSearchable = [
        'name'  => 'like',
        'email' => '=',
    ];
}

Usage:

<?php

// paginate the data by 10
$users = $userRepository->paginate(10);

// search by 1 field
$cars = $carRepository->findByField('colour', $colour);

// searching multiple fields
$offer = $offerRepository->findWhere([
    'offer_id' => $offer_id,
    'user_id'  => $user_id,
])->first();

//....

Note: If the Repository belongs to Model with a name different than its Container name, the Repository class of that Model must set the property $container and define the Container name.

Example:

<?php

namespace App\Containers\Authorization\Data\Repositories;

use App\Ship\Parents\Repositories\Repository;

class RoleRepository extends Repository
{
    protected $container = 'Authorization'; // the container name. Must be set when the model has different name than the container

    protected $fieldSearchable = [

    ];

}

Other Properties:

API Query Parameters Property

To enable query parameters (?search=text,…) in your API you need to set the property $fieldSearchable on the Repository class, to instruct the querying on your model.

Example $fieldSearchable of a Repository:

 <?php

    protected $fieldSearchable = [
      'name'  => 'like',
      'email' => '=',
    ];

Continue reading to find more about those properties and what they do.

All other Properties

apiato uses the andersao/l5-repository package, to provide a lot of powerful features to the repository class. such as

<?php

     // ...

    protected $cacheMinutes = 1440; // 1 day

    protected $cacheOnly = ['all'];

To learn more about all the properties you can use, visit the andersao/l5-repository package documentation.

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
发起讨论 只看当前版本


暂无话题~