gohari/repository-pattern
Composer 安装命令:
composer require gohari/repository-pattern
包简介
A Laravel package to generate and use clean repository pattern classes with Artisan commands.
README 文档
README
Build clean repositories for Laravel models with one Artisan command.
gohari/repository-pattern provides a reusable BaseRepository, a matching BaseRepositoryInterface, and a generator command that creates repository classes inside the Laravel application that installs your package.
Current release: v1.1.0
Documentation: mohammadrezagohari.github.io/RepositoryPattern
Features
- Artisan repository and service layer generator
- Automatic Laravel package discovery
- Automatic model creation for missing
App\Models\...classes - Repository and interface stubs
- Custom output path support
- Optional automatic interface binding
- Optional Service, ServiceInterface, DTO, and config generation
- Relationship loading, sorting, and soft delete helpers
- Shared base methods for common Eloquent operations
- Backward-compatible method aliases like
insertData,updateItem, anddeleteData - PHPUnit and Orchestra Testbench coverage for package behavior
Requirements
- PHP
^8.3 - Laravel
^10.0,^11.0,^12.0, or^13.0
Installation
Install the package with Composer:
composer require gohari/repository-pattern
Laravel will discover the service provider automatically:
Gohari\RepositoryPattern\RepositoryPatternServiceProvider::class
If package discovery is disabled in your app, register the provider manually in config/app.php:
'providers' => [ Gohari\RepositoryPattern\RepositoryPatternServiceProvider::class, ],
Publish the config or generator stubs when you want to customize package defaults:
php artisan vendor:publish --tag=repository-pattern-config php artisan vendor:publish --tag=repository-pattern-stubs
Published stubs live in:
resources/stubs/vendor/repository-pattern
Quick Start
Create a repository for a model:
php artisan repository:make User --model=User
This creates:
app/Repositories/UserRepository.php
app/Repositories/Contracts/UserRepositoryInterface.php
If App\Models\User does not exist, the package will create it with Laravel's make:model command.
Command Usage
php artisan repository:make {name} --model={model}
Arguments and options:
| Name | Required | Description |
|---|---|---|
name |
Yes | Repository name. User and UserRepository both generate UserRepository. |
--model |
No | Model class name or FQCN. Defaults to the repository name. |
--path |
No | Output directory. Defaults to app/Repositories. |
--interface-path |
No | Repository interface output directory. Defaults to app/Repositories/Contracts. |
--force |
No | Overwrite existing repository files. |
--bind |
No | Bind the generated interface to the repository in App\Providers\RepositoryServiceProvider. |
--service |
No | Generate Service, ServiceInterface, DTO, and config alongside the repository. |
--service-path |
No | Service output directory. Defaults to app/Services. |
--dto |
No | Generate only the DTO alongside the repository. |
--dto-path |
No | DTO output directory. Defaults to app/DTOs. |
--config |
No | Copy config/repository-pattern.php into the application. |
Examples:
php artisan repository:make User --model=User
php artisan repository:make UserRepository --model=User
php artisan repository:make Product --model="App\Models\Product"
php artisan repository:make Admin/User --model=User
php artisan repository:make User --model=User --service
Use a custom output path:
php artisan repository:make Product --model=Product --path=app/Domain/Repositories
Overwrite existing files:
php artisan repository:make Product --model=Product --force
Create files and register the interface binding:
php artisan repository:make User --model=User --bind
Generate repository and service layers together:
php artisan repository:make User --model=User --service --bind
For fully-qualified model classes outside App\Models, the command will warn you if the model file is missing, but it will not generate it automatically.
Generated Repository
For this command:
php artisan repository:make User --model=User
The package generates:
<?php namespace App\Repositories; use Gohari\RepositoryPattern\BaseRepository; use App\Models\User; use App\Repositories\Contracts\UserRepositoryInterface; class UserRepository extends BaseRepository implements UserRepositoryInterface { public function __construct(User $user) { parent::__construct($user); } }
And:
<?php namespace App\Repositories\Contracts; use Gohari\RepositoryPattern\BaseRepositoryInterface; interface UserRepositoryInterface extends BaseRepositoryInterface { // }
Base Repository Methods
Every generated repository extends BaseRepository, so these methods are available immediately:
$repository->query(); $repository->getAll(); $repository->paginate(15); $repository->findById($id); $repository->findOrFail($id); $repository->firstWhere('email', 'user@example.com'); $repository->findBy('email', 'user@example.com'); $repository->exists($id); $repository->count(); $repository->create($data); $repository->updateOrCreate(['email' => $email], $data); $repository->update($id, $data); $repository->delete($id); $repository->deleteMany([$firstId, $secondId]); $repository->search('email', 'user@example.com', '='); $repository->searchByColumn('name', 'john'); $repository->with(['roles', 'profile'])->paginate(); $repository->withRelations(['roles', 'profile'])->get(); $repository->sortBy('created_at', 'desc')->get(); $repository->withTrashed()->getAll(); $repository->onlyTrashed()->count(); $repository->restore($id); $repository->forceDelete($id);
Backward-compatible aliases:
$repository->insertData($data); $repository->updateItem($id, $data); $repository->deleteData($id);
Generated Service Layer
Use --service to generate Repository, RepositoryInterface, Service, ServiceInterface, DTO, and config in one command:
php artisan repository:make User --model=User --service
This creates:
app/Repositories/UserRepository.php
app/Repositories/Contracts/UserRepositoryInterface.php
app/Services/UserService.php
app/Services/UserServiceInterface.php
app/DTOs/UserData.php
config/repository-pattern.php
The generated service supports pagination with relationships and sorting, plus create/update through a DTO or array:
$users->paginate(['roles'], 'created_at', 'desc', 20); $users->find($id, ['profile']); $users->create(UserData::fromArray($data)); $users->restore($id); $users->forceDelete($id);
Binding Interfaces
You can bind repositories automatically with --bind:
php artisan repository:make User --model=User --bind
This creates or updates app/Providers/RepositoryServiceProvider.php:
public function register(): void { $this->app->bind( \App\Repositories\Contracts\UserRepositoryInterface::class, \App\Repositories\UserRepository::class ); }
The command also registers App\Providers\RepositoryServiceProvider::class in bootstrap/providers.php for modern Laravel apps, or in config/app.php when that is the available provider registration file.
Configuration
After publishing repository-pattern-config, you can control paths, namespaces, and default generator behavior:
return [ 'paths' => [ 'repositories' => app_path('Repositories'), 'interfaces' => app_path('Repositories/Contracts'), 'services' => app_path('Services'), ], 'namespaces' => [ 'repositories' => 'App\\Repositories', 'interfaces' => 'App\\Repositories\\Contracts', 'services' => 'App\\Services', ], 'auto_bind' => true, 'generate_service' => false, 'generate_model_if_missing' => true, ];
Then inject the interface anywhere:
use App\Repositories\Contracts\UserRepositoryInterface; class UserService { public function __construct( private readonly UserRepositoryInterface $users ) { } public function activeUsers() { return $this->users ->query() ->where('is_active', true) ->get(); } }
Custom Repository Methods
Add domain-specific methods to the interface:
use Gohari\RepositoryPattern\BaseRepositoryInterface; interface UserRepositoryInterface extends BaseRepositoryInterface { public function findByEmail(string $email); }
Then implement them in the repository:
use App\Models\User; use App\Repositories\Contracts\UserRepositoryInterface; use Gohari\RepositoryPattern\BaseRepository; class UserRepository extends BaseRepository implements UserRepositoryInterface { public function __construct(User $user) { parent::__construct($user); } public function findByEmail(string $email) { return $this->query()->where('email', $email)->first(); } }
Testing
Install development dependencies and run the package test suite:
composer install
composer test
composer analyse
composer format:test
The tests cover:
- Repository generator command
- Package service provider command registration
- Generated repository and interface contents
- Missing model generation for
App\Models --forceoverwrite behavior--bind,--service, DTO, config, and custom stub generation- Shared
BaseRepositoryCRUD/query behavior - Search, relationship loading, sorting, soft delete, and fluent repository scopes
- Legacy aliases:
insertData,updateItem,deleteData
Package Development
For packages, vendor/ and composer.lock should stay out of the repository. Install dependencies locally when developing:
composer install
Before tagging a release, run:
composer validate --strict --no-check-lock
composer test
composer analyse
composer format:test
For Packagist, push your repository to GitHub, submit the package, and tag releases with semantic versions:
git tag v1.1.0 git push origin v1.1.0
Security
Please review SECURITY.md before reporting vulnerabilities.
License
The MIT License. See LICENSE for more information.
gohari/repository-pattern 适用场景与选型建议
gohari/repository-pattern 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 3, 最近一次更新时间为 2026 年 06 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「repository」 「laravel」 「eloquent」 「artisan」 「repository-pattern」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gohari/repository-pattern 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gohari/repository-pattern 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gohari/repository-pattern 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Laravel 5 - Repositories to the database layer
Structure for the Laravel Service-Repository Pattern
Rinvex Repository is a simple, intuitive, and smart implementation of Active Repository with extremely flexible & granular caching system for Laravel, used to abstract the data layer, making applications more flexible to maintain.
Base Skeleton for Laravel Application
Generates a trait to help ease the access of custom repo methods
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-17