sagni / repository
这是用于创建仓库模式的Laravel包
Requires
- php: ^8.0
- illuminate/console: ^8.0|^9.0|^10.0
- illuminate/support: ^8.0|^9.0|^10.0
- symfony/console: ^5.0|^6.0
This package is auto-updated.
Last update: 2024-09-24 18:04:39 UTC
README
概览
这是一个轻量级的Laravel包,旨在简化在项目中创建仓库设计模式的流程。使用这个包,您可以轻松生成必要的文件,包括具体类、接口和服务提供者。
安装
要求
该包已经开发并测试,以下为最低要求
- PHP 8.0
- Laravel 8
安装包
运行以下命令
composer require sagni/repository
使用
基本使用
要使用make:repository命令创建仓库,只需运行
php artisan make:repository User/UserRepository
如果您的应用目录中不存在Repository目录,命令将自动为您创建。上面的命令将生成以下文件
- app/Repository/User/UserRepository.php
- app/Repository/User/UserRepositoryInterface.php
- app/Providers/User/UserRepositoryServiceProvider.php
如果您希望将生成的仓库文件直接保存在app/Repository目录中而不创建子目录,可以使用以下命令
php artisan make:repository UserRepository
此命令生成上述相同的文件,但不包含User子目录。
仓库类
生成必要文件后,您需要实现您的仓库逻辑。打开在app/Repository/User目录中创建的UserRepository.php文件,并根据需要修改它
<?php namespace App\Repositories\User; use App\Repositories\User\UserRepositoryInterface; class UserRepository implements UserRepositoryInterface{ public function __construct(){ } }
仓库接口
编辑app/Repository/User目录中的UserRepositoryInterface.php文件以定义仓库方法
<?php namespace App\Repositories\User; Interface UserRepositoryInterface{ }
服务提供者
位于app/Providers/User目录中的UserRepositoryServiceProvider.php文件将仓库接口绑定到其实例。您不需要修改此文件,因为它会自动生成。
<?php namespace App\Repositories\User; use Illuminate\Support\ServiceProvider; use App\Repositories\User\UserRepository; use App\Repositories\User\UserRepositoryInterface; class UserRepositoryServiceProvider extends ServiceProvider{ public function register(){ $this->app->bind(UserRepositoryInterface::class,UserRepository::class); } public function boot(){ } }
以下内容将自动添加到项目的config/app.php中
App\Repositories\User\UserRepositoryServiceProvider::class,
贡献
如果您希望对包进行任何修改或改进,请随时提出pull request。
许可协议
MIT许可协议(MIT)。请参阅许可文件以获取更多信息。
结论
Sagni Repository包大大简化了在Laravel应用程序中创建仓库类的过程。通过自动化生成仓库文件、接口和服务提供者,它鼓励采取结构化和组织化的数据访问方法。您可以自由扩展基本实现以满足项目的特定需求。
如果您有任何问题、反馈或贡献,请随时联系包作者
- 作者: Sagni Alemayehu
- 电子邮件: sagnialemayehu69@gmail.com