hoovhai/laravel-basic-repository

一个用于实现简单仓库设计模式的包

dev-master 2023-03-20 07:48 UTC

This package is auto-updated.

Last update: 2024-09-20 10:58:02 UTC


README

composer require hoovhai/laravel-basic-repository

或者在 composer.json 中添加并运行 composer install

"require-dev": {
    "hoovhai/laravel-basic-repository": "dev-master"
},

步骤 2

创建 app/Providers/CommandsServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class CommandsServiceProvider extends ServiceProvider
{
    public function boot()
    {
        if ($this->app->runningInConsole()) {
            $this->commands([
                \Hoovhai\Repositories\Commands\MakeRepositoryCommand::class,
                \Hoovhai\Repositories\Commands\GenerateContractCommand::class,
                \Hoovhai\Repositories\Commands\GenerateControllerCommand::class,
                \Hoovhai\Repositories\Commands\GenerateRepositoryCommand::class,
                \Hoovhai\Repositories\Commands\GenerateServiceProviderCommand::class,
            ]);
        }
    }
}

步骤 3

添加到 config\app.php

'providers' => [
    ...
    App\Providers\CommandsServiceProvider::class,
    ...
],

步骤 4

运行 php artisan make:repository {name}

示例: php artisan make:repository User

步骤 5

添加到 config\app.php

'providers' => [
    ...
    App\Providers\RepositoriesServiceProvider::class,
    ...
],

示例

// add to route web or api
Route::get('/index', 'UserController@index')->name('getAllUser');
// add to controller
public function index()
{
    return $this->repo->index();
}
// add to contract
public function index();
// add to your repository
public function index()
{
    // code here
}

当你在控制器中调用 index 方法时,这将转发到 RepositoriesServiceProvider 中的 repositoryindex 方法