nfunwigabga / lara-repo
在 Laravel 应用中生成仓库
Requires
- php: >=7.2
- illuminate/support: >=6.0
This package is auto-updated.
Last update: 2024-09-18 09:00:28 UTC
README
Laravel 6.0 及以上版本的 Laravel 仓库生成器。
安装
使用 Composer 安装此包
composer require Nfunwigabga/lara-repo
Laravel 使用包自动发现,因此此包不需要您手动注册服务提供者。
不使用自动发现
如果您不使用自动发现,则需要在 config/app.php
文件中的 Providers 数组中添加以下服务提供者
Nfunwigabga\LaraRepo\ServiceProvider::class
发布配置
该包包含一个配置文件,您可以使用以下命令发布
php artisan vendor:publish --provider="Nfunwigabga\LaraRepo\ServiceProvider"
配置定义了仓库和模型的目录结构。
用法
该包提供了两个命令,允许您生成仓库和标准。
生成仓库
每个仓库基于一个模型类。因此,当您生成仓库时,您必须提供模型类名称(不包含子目录)
php artisan make:repo {model name}
例如,如果您想为用户模型创建仓库,您可以这样做
php artisan make:repo User
如果模型尚未存在,系统将提示您是否同时创建它。
上述命令将生成以下文件
// This is the concrete implementation of the repository
app/Repositories/UserRepository.php
// This is the interface that the repository class binds to.
app/Repositories/Contracts/IUser.php
// The model class if a User model did not exist before
app/User.php
// If you chose to generate a migration
database/migrations/xx_xx_xx_create_users_table.php
在控制器中使用仓库
一旦生成了仓库,您就可以直接调用一些基础方法。例如,在 UserController
类中,您可以像这样注入仓库合约并调用其中的方法
<?php namespace App\Http\Controllers; use App\Repositories\Contracts\IUser; class UserController extends Controller { public $userRepo; /** * Inject the repository interface inside the constructor **/ public function __construct(IUser $userRepo) { $this->userRepo = $userRepo; } public function index() { $users = $this->userRepo->all(); } public function show($id) { $user = $this->userRepo->find($id); } }
该包提供了许多基础方法,您可以直接访问(如我们上面所做的那样);这些方法可以在 BaseRepository 类 中找到。
但是,您可以在各个仓库类中创建更多方法,特别是特定于该模型类的特定方法。例如
<?php namespace App\Repositories; use App\User; use App\Repositories\Contracts\IUser; use Nfunwigabga\LaraRepo\Base\BaseRepository; class UserRepository extends BaseRepository implements IUser { public function model() { return User::class; } public function subscriptToNewsletter($userId) { // We can use the find method in the Base repository here to get the user $user = $this->find($userId); // my logic to subscribe the user to a newsletter } }
如果您在仓库中添加了新方法,您还必须在相应的接口中添加该方法
<?php namespace App\Repositories\Contracts; interface IUser { public function subscribeToNewsletter(int $userId); }
一旦该方法定义好,您就可以像我们之前做的那样在控制器中调用它。
标准
该包使用标准类来向查询添加更多过滤器(使查询更加灵活)。该包提供了一些预先创建的标准类,您可以使用它们,但如果需要,您还可以创建更多。以下是一些基础标准
预加载
用户
最新优先
包含已删除的
这是您如何在查询中使用标准的方法
<?php namespace App\Http\Controllers; use Nfunwigabga\LaraRepo\Eloquent\Criteria\EagerLoad; // import the namespaces use Nfunwigabga\LaraRepo\Eloquent\Criteria\LatestFirst; use App\Repositories\Contracts\IUser; class UserController extends Controller { public $userRepo; /** * Inject the repository interface inside the constructor **/ public function __construct(IUser $userRepo) { $this->userRepo = $userRepo; } public function index() { // Get all users and eagerload their related posts and comments // Don't forget to import the namespaces at the top $users = $this->userRepo->withCriteria( new EagerLoad(['posts', 'post.comments']), // this is the Criteria class to eagerliad new LatestFirst() // order by latest first )->all(); } }
生成新的标准
您还可以使用命令生成新的标准
php artisan make:criteria {Criteria name}
例如
php artisan make:criteria IsLive
这将生成一个类
app/Repositories/Eloquent\Criteria/IsLive.php
标准类只有一个方法:apply 方法。在上面的示例中,它可以
<?php namespace App\Repositories\Criteria; use Nfunwigabga\LaraRepo\Base\ICriterion; class IsLive implements ICriterion { public function __construct() { } public function apply($model) { return $model->whereNotNull('published_at'); } }
然后,这个标准类就可以在您的控制器中使用,就像我们上面所做的那样。