jeishanul/laravel-repository-generator
Composer 安装命令:
composer require jeishanul/laravel-repository-generator
包简介
A Laravel package to generate Repository pattern classes and auto-bind them.
README 文档
README
A simple Laravel Artisan command to scaffold the Repository pattern: generates an Interface in app/Interfaces, a concrete Repository in app/Repositories, and auto-binds them in AppServiceProvider.php. Supports nested namespaces (e.g., Api\Admin\User).
Features
- Generates CRUD-ready Interface and Repository classes.
- Handles nested paths (e.g.,
app/Interfaces/Api/Admin/UserInterface.php). - Automatic IoC binding for dependency injection.
- Overwrite protection with
--forceflag. - Compatible with Laravel 10, 11, and 12.
Installation
You can install the package via Composer:
composer require jeishanul/laravel-repository-generator
The package will auto-discover its service provider. If not, add it manually to config/app.php under providers:
'providers' => [ // ... Jeishanul\RepositoryGenerator\RepositoryGeneratorServiceProvider::class, ],
Usage
Run the command to generate files for a given repository:
php artisan make:repository User
Or with nested namespaces:
php artisan make:repository Api\Admin\User
Options
--force: Overwrite existing files.
Generated Files
For php artisan make:repository Api\Admin\User:
-
Interface:
app/Interfaces/Api/Admin/UserInterface.php<?php namespace App\Interfaces\Api\Admin; interface UserInterface { /** * Get all users. */ public function all(); /** * Find a user by ID. */ public function find($id); /** * Create a new user. */ public function create(array $data); /** * Update a user. */ public function update($id, array $data); /** * Delete a user. */ public function delete($id); }
-
Repository:
app/Repositories/Api/Admin/UserRepository.php<?php namespace App\Repositories\Api\Admin; use App\Interfaces\Api\Admin\UserInterface; use App\Models\User; use Illuminate\Database\Eloquent\Collection; class UserRepository implements UserInterface { protected $model; public function __construct(User $model) { $this->model = $model; } public function all(): Collection { return $this->model->all(); } public function find($id) { return $this->model->findOrFail($id); } public function create(array $data): User { return $this->model->create($data); } public function update($id, array $data): bool { $model = $this->find($id); return $model->update($data); } public function delete($id): bool { $model = $this->find($id); return $model->delete(); } }
-
Auto-Binding: Appended to
app/Providers/AppServiceProvider.phpin theregister()method:public function register() { // ... existing code ... $this->app->bind(App\Interfaces\Api\Admin\UserInterface::class, App\Repositories\Api\Admin\UserRepository::class); }
Using in Controllers
Inject the interface for loose coupling:
<?php namespace App\Http\Controllers; use App\Interfaces\Api\Admin\UserInterface; class UserController extends Controller { protected $userRepo; public function __construct(UserInterface $userRepo) { $this->userRepo = $userRepo; } public function index() { return $this->userRepo->all(); } public function store(Request $request) { return $this->userRepo->create($request->validated()); } // ... other methods }
Customization
- Stubs: Override stubs by publishing them (future feature) or editing
src/Stubs/in your fork. - Model Path: Assumes flat models (e.g.,
App\Models\User). For nested models, update the stub's import. - Bindings: Uses
AppServiceProviderfor simplicity. For production, consider a dedicatedRepositoryServiceProvider.
Testing
composer test
(Tests use PHPUnit; add your own for custom stubs.)
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Thank you for considering contributing! Please follow these steps:
- Fork the repo on GitHub.
- Create a feature branch (
git checkout -b feature/new-feature). - Commit changes (
git commit -am 'Add new feature'). - Push to the branch (
git push origin feature/new-feature). - Open a Pull Request.
Security Vulnerabilities
If you discover a security vulnerability, please email shishirjeishanul@gmail.com instead of opening an issue.
License
The MIT License (MIT). Please see License File for more information.
Credits
- Jeishanul Haque Shishir
- Built with ❤️ for the Laravel community.
⭐ Star this repo if it helps your project! Follow @jeishanul on GitHub for more packages.
jeishanul/laravel-repository-generator 适用场景与选型建议
jeishanul/laravel-repository-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「generator」 「repository」 「laravel」 「artisan」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jeishanul/laravel-repository-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jeishanul/laravel-repository-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jeishanul/laravel-repository-generator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
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
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-29