unostentatious / 存储库
一个抽象层,允许您为您的模型实现存储库模式。
v1.3.1
2023-09-12 14:52 UTC
Requires
- php: >=7.4
Requires (Dev)
- laravel/lumen-framework: ^9.0
- mockery/mockery: ^1.3.1
- phpunit/phpunit: ^9.3
This package is auto-updated.
Last update: 2024-09-12 17:02:23 UTC
README
Unostentatious 存储库
一个抽象层,允许您为您的模型实现存储库模式。
要求
- PHP 7.4^
- Laravel 8.x / Lumen 8.x
安装
步骤 1:通过 Composer 安装
composer require unostentatious/repository
步骤 2:发布服务提供者
在 Laravel 中
- 在
Laravel
中,编辑config\app.php
并在包服务提供者部分添加提供者
/* * Package Service Providers... */ \Unostentatious\Repository\Integration\Laravel\UnostentatiousRepositoryProvider::class,
- 然后打开您的终端,在
Laravel
应用程序的根目录下,发布供应商
php artisan vendor:publish --provider="Unostentatious\Repository\Integration\Laravel\UnostentatiousRepositoryProvider"
在 Lumen 中
- 将
unostent-repository.php
配置文件从vendor/unostentatious/repository/Integration/config/
目录复制过来 - 如果您的 Lumen 应用程序中不存在
{root}/config/
目录,请确保首先创建它,然后粘贴您刚刚复制的配置文件 - 编辑
bootstrap/app.php
然后注册服务提供者并显式添加包的配置,如下所示
// Other actions... $app->register(\Unostentatious\Repository\Integration\Laravel\UnostentatiousRepositoryProvider::class); $app->configure('unostent-repository');
步骤 3:自定义配置
目前包的配置已经位于您的应用程序的配置目录 /config
中,您可以根据需要自定义包的配置中的 3 个值
<?php declare(strict_types=1); return [ 'root' => null, 'destination' => null, 'placeholder' => null ];
安装完成
哇!就这样,您就可以在 Laravel 或 Lumen 应用程序中使用 Unostentatious 存储库
了,祝您编码愉快!
用法
在创建存储库类时,它 必须 位于指定的路径 {root}/{destination}/{placeholder}
,在这种情况下,默认路径将是 app/Database/Repositories
步骤 1:创建存储库
它必须由一个 具体 类及其对应的 接口 组成。
步骤 2:遵循约定
然后具体类 必须 扩展包中的 AbstractEloquentRepository。
请参阅示例
<?php namespace App\Database\Repositories; use App\Database\Repositories\Interfaces\UserRepositoryInterface; use Unostentatious\Repository\AbstractEloquentRepository; class UserRepository extends AbstractEloquentRepository implements UserRepositoryInterface { // Business logic goes here. }
步骤 3:将它们加载到 IoC 中
然后在这些类编写完毕后,只需执行 composer dump-autoload
以调用 IoC,这些类现在可注入到消费类中,例如 控制器
。