duardaum / laravel-repository
一个简单的Repository和Service层,适用于Laravel和Lumen应用
Requires
- php: >=8.0
- illuminate/contracts: >=8.0
- illuminate/database: >=8.0
- illuminate/support: >=8.0
Requires (Dev)
- orchestra/testbench: ^6.41
This package is auto-updated.
Last update: 2024-09-29 21:22:09 UTC
README
一个简单的Repository和Service层,适用于Laravel和Lumen应用
兼容性
安装
使用Composer安装此包
composer require duardaum/laravel-repository
配置
在您的Laravel/Lumen应用中,创建一个新的Service Provider。
这个Service Provider将是您注册所有Repository的地方。
如果您愿意,可以在 AppServiceProvider 中注册您的repository,但我们建议将其分开,以便Service Provider可能非常大。
namespace App\Providers; use Illuminate\Support\ServiceProvider; class RepositoryServiceProvider extends ServiceProvider { public function register() { // } public function boot() { // } }
然后,在您的应用程序中注册新的Service Provider
在Lumen中
//bootstrap/app.php $app->register(App\Providers\RepositoryServiceProvider::class);
在Laravel中
//config/app.php ... 'providers' => [ ... App\Providers\RepositoryServiceProvider::class, ] ...
使用方法
Repository模式是一种设计模式,随着岁月的流逝变得非常流行。这是一种非常不错且有效的方法,可以将数据访问和逻辑组织在同一个地方,同时让应用的其他部分负责它们最擅长的事情,特别是如果您正在使用其他设计模式如S.O.L.I.D和Clean Code。
考虑到这一点,我们创建了这个非常简单但功能强大的包,让您能够以非常简单的方式集中管理所有数据访问。
模型、Repository与接口
假设我们有一个名为 messages 的表,具有以下结构,我们将在其上运行一些SQL命令
CREATE TABLE messages ( id INT PRIMARY KEY, content VARCHAR(255) NOT NULL, created_at DATETIME NULL, updated_at DATETIME NULL, deleted_at DATETIME NULL );
首先,让我们创建一个模型
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class Message extends Model { use SoftDeletes; protected $table = 'messages'; protected $fillable = [ 'content', 'created_at', 'updated_at', ]; }
然后,让我们为Repository创建一个接口。所有接口 必须 从 BaseRepositoryInterface 继承
namespace App\Contracts\Repositories; use Duardaum\LaravelRepository\Contracts\Repositories\BaseRepositoryInterface; interface MessageRepositoryInterface extends BaseRepositoryInterface { }
然后,让我们创建一个Repository。所有repository 必须 从 BaseRepository 继承,并实现一个从 BaseRepositoryInterface 继承的接口。
为了让repository知道它们应该运行哪些查询,我们需要为repository提供一个模型
namespace App\Repositories; use Duardaum\LaravelRepository\Repositories\BaseRepository; use App\Contracts\Repositories\MessageRepositoryInterface; use App\Models\Message; class MessageRepository extends BaseRepository implements MessageRepositoryInterface { protected string|\Illuminate\Database\Eloquent\Model $_model = Message::class; }
最后,让我们注册这个repository以在应用程序中使用
// app/Providers/RepositoryServiceProvider.php ... public function register() { $this->app->bind(\App\Contracts\Repositories\MessageRepositoryInterface::class, \App\Repositories\MessageRepository::class); } ...
这种Repository与接口的结构非常有帮助,如果您需要为相同的repository创建不同的实现。repository的契约保持不变,不会影响您的应用,您只需在一个地方替换即可。
基本用法
您可以将接口注入到构造函数/方法中以实例化repository,或者使用服务容器来执行此操作
// 1. On constructor use App\Contracts\Repositories\MessageRepositoryInterface; class SomeClass { public function __construct ( private MessageRepositoryInterface $_repo_message ){} } // 2. With Service Container $repo_message = app(\App\Contracts\Repositories\MessageRepositoryInterface::class);
创建、更新和删除
进行这些操作非常简单
// create $message = $repo_message->create(['content' => 'Test 1']); //update $repo_message->update(['content' => 'Test 2'], $message->id); //Delete - soft $repo_message->delete($message->id); //Delete - hard $repo_message->forceDelete($message->id);
读取
这个包的一个最伟大的特性是,您可以在同一操作中轻松读取不同状态(激活、停用、两者)的数据。默认情况下,repository只会获取活动记录(newQuery)的数据。要获取停用(停用)或两者,有一个简单的方法可以做到这一点
//From trash (deactivate) $data = $repo_message->onlyTrashed()->findWhere(...); //The next line will keep on trashed context, so you don't need to put 'onlyTrashed' again: $data2 = $repo_message->findWhere(...); //If you want to change query context on next line, just call 'newQuery' or 'withTrashed' and the context will change: $data3 = $repo_message->newQuery()->findWhere(...); $data4 = $repo_message->findWhere(...); // are running on 'newQuery' context
自定义方法
在repository中创建自定义方法非常简单,您可以利用基础repository内置的方法来实现这一点
// app/Repositories/MessageRepository.php ... public function getAllDeactived(): \Illuminate\Database\Eloquent\Collection { self::onlyTrashed()->all(); } .. // app/Contracts/Repositories\MessageRepositoryInterface.php ... public function getAllDeactived() : \Illuminate\Database\Eloquent\Collection; ...
您还可以创建一个自定义方法,同时考虑用户选择的查询上下文
// app/Repositories/MessageRepository.php ... public function someGreatMethod(): \Illuminate\Database\Eloquent\Collection { return self::getCurrentQuery()->findWhere(...); } ... // app/Contracts/Repositories/MessageRepositoryInterface.php ... public function someGreatMethod(): \Illuminate\Database\Eloquent\Collection; ...
贡献
欢迎提交拉取请求。对于重大更改,请先打开一个问题来讨论您想要更改的内容。
请确保适当更新测试。