dlx-llc/laravel-repository

Laravel框架的仓库设计模式实现。

3.0.1 2024-08-05 10:32 UTC

This package is auto-updated.

Last update: 2024-09-05 10:38:53 UTC


README

Laravel Version Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

什么是仓库?

仓库通过一个类似于集合的接口,在领域和数据映射层之间进行中介,用于访问领域对象。在这个包中,你可以找到其在Laravel框架中的实现。

何时使用它

在一个具有许多领域对象类型和许多可能查询的大系统中,仓库可以减少处理所有查询所需的代码量。仓库提倡使用规范模式(以准则对象的形式),它封装了要执行的查询。此外,在存在多个数据源的情况下,仓库也非常有用。

安装

使用Composer安装此包

composer require dlx-llc/laravel-repository

此包使用自动发现来注册其服务提供者。但是,如果您已禁用此包的自动发现,则需要手动注册服务提供者

Deluxetech\LaRepo\LaRepoServiceProvider::class

此包有自己的异常和验证错误消息翻译。您可以通过遵循Laravel文档中描述的步骤来覆盖它们。

还有一些配置您可能需要替换为自己。在这种情况下,您可以使用以下命令发布配置

php artisan vendor:publish --tag=larepo-config

版本控制

此包是为了与Laravel的最新版本一起使用而构建的,但它应与版本 >= 10.x 一起正常工作。

使用方法

您可以通过将Laravel模型类名传递给其构造函数来使用通用的EloquentRepository类。还有一个LaRepo外观,它提供了一组方法,这些方法可以帮助您节省编写一些重复代码的时间。

use Deluxetech\LaRepo\Facades\LaRepo;
use Deluxetech\LaRepo\Eloquent\GenericRepository;
use Deluxetech\LaRepo\Contracts\RepositoryContract;

class UserController
{
    protected RepositoryContract $repo;

    public function __construct()
    {
        $this->repo = new GenericRepository(User::class);
    }

    public function index()
    {
        return LaRepo::getManyWithRequest($this->repo);
    }
}

运行Docker容器

构建镜像

docker build -t dlx-llc-laravel-repository .

使用此命令(重新)启动Docker容器

docker rm -f dlx-llc-laravel-repository 2> /dev/null && \
docker run -itd \
    --name dlx-llc-laravel-repository \
    --mount type=bind,source="$(pwd)"/.,target=/home/larepo/app \
    --restart on-failure:5 \
    dlx-llc-laravel-repository