wimski/laravel-model-repositories
Composer 安装命令:
composer require wimski/laravel-model-repositories
包简介
Laravel model repositories
关键字:
README 文档
README
Laravel Model Repositories
Generic repository pattern for Laravel Eloquent models.
Changelog
Usage
Install package
composer require wimski/laravel-model-repositories
Setup service provider
This package is NOT loaded using package discovery. You should create your own service provider that extends the one from this package and add it to your config manually.
app/Providers/RepositoryServiceProvider.php
<?php namespace App\Providers; use Wimski\ModelRepositories\Providers\ModelRepositoryServiceProvider; class RepositoryServiceProvider extends ModelRepositoryServiceProvider { protected array $repositories = [ // add your repository bindings here ]; }
config/app.php
<?php return [ 'providers' => [ /* * Application Service Providers... */ App\Providers\RepositoryServiceProvider::class, ], ];
Generate a repository
php artisan make:repository App\\Models\\MyModel
This will create the following files:
MyModelRepositoryInterface.phpMyModelRepository.php
The namespace and file location depend on the namespace configuration.
Alternatively you can completely override the contract and repository FQN by using the command options. The namespace matching and class name suffixing will be skipped.
php artisan make:repository App\\Models\\MyModel --contract=Foo\\Bar --repository=Lorem\\Ipsum
Add repository binding
Set up the binding of your new repository in the service provider.
protected array $repositories = [ MyModelRespositoryInterface::class => MyModelRespository::class, ];
Example usage
class SomeService { protected MyModelRespositoryInterface $repository; public function __construct(MyModelRespositoryInterface $repository) { $this->repository = $repository; } public function doSomething($id): void { $myModel = $this->repository->findOrFail($id); } }
Publishing configuration file
Because this package does not register its own service provider, your own service provider should be referenced when publishing this package's configuration file.
php artisan vendor:publish --provider=App\Providers\RepositoryServiceProvider
Namespace configuration
The namespaces configuration is used to determine what the namespaces of your repository classes - and locations of the subsequent files - should be. Each configuration exists of three parts:
modelscontractsrepositories
When generating a repository for a model the command will look for a namespace configuration which has a models part that matches the supplied model.
The contracts and repositories parts of that configuration are then used as the namespaces for the repository classes.
Assumptions
- The namespaces follow the PSR-4 file location convention.
- The namespaces are within the Laravel app directory.
Default configuration
'namespaces' => [ [ 'models' => 'App\\Models', 'contracts' => 'App\\Contracts\\Repositories', 'repositories' => 'App\\Repositories', ], ],
When generating a repository for App\Models\MyModel, the following two classes will be created:
App\Contracts\Repositories\MyModelRepositoryInterfaceApp\Repositories\MyModelRepository
Multiple configurations
You can have multiple namespace configurations, for example when using domains.
'namespaces' => [ [ 'models' => 'App\\DomainA\\Models', 'contracts' => 'App\\DomainA\\Contracts\\Repositories', 'repositories' => 'App\\DomainA\\Repositories', ], [ 'models' => 'App\\DomainB\\Models', 'contracts' => 'App\\DomainB\\Contracts\\Repositories', 'repositories' => 'App\\DomainB\\Repositories', ], ],
Specificity
The first match will be used so if you have overlapping namespace configurations, make sure to have the more specific ones on top.
'namespaces' => [ [ 'models' => 'App\\Models\\SpecificModels', ... ], [ 'models' => 'App\\Models', ... ], ],
Available methods
See interface.
Stub customization
See Laravel's documentation about stub customization. This package adds the following stub files:
model.repository.interface.stubmodel.repository.stub
PHPUnit
composer test
PHPStan
composer analyze
Credits
License
The MIT License (MIT). Please see License File for more information.
wimski/laravel-model-repositories 适用场景与选型建议
wimski/laravel-model-repositories 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 37.17k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 01 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「repository」 「model」 「laravel」 「wimski」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wimski/laravel-model-repositories 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wimski/laravel-model-repositories 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wimski/laravel-model-repositories 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Interfaces for web resource models and services to retrieve and create them
Laravel 5 - Repositories to the database layer
This package provides an Address Model, Migration and Factory. It also provides the traits HasBillingAddres and HasPublicAddress
Structure for the Laravel Service-Repository Pattern
统计信息
- 总下载量: 37.17k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-01-11