derakht/repository-pattern

使用命令实现仓库模式设计

V1.0.0 2020-09-21 14:39 UTC

This package is auto-updated.

Last update: 2024-09-23 00:58:15 UTC


README

使用单个命令生成仓库结构

此包生成与模型相关的仓库模式文件,以便在您的应用程序中使用。

安装

使用以下命令通过composer安装此包

composer require derakht/repository-pattern

配置

使用以下命令发布配置文件。

php artisan vendor:publish --provider="Derakht\RepositoryPattern\RepositoryPatternServiceProvider"

您可以在该文件中更改默认路径。

用法

现在,您可以使用以下命令创建仓库文件。

php artisan make:repository ModelName

示例

php artisan make:repository Post

PostController.php

<?php

namespace App\Http\Controllers;

use App\Repositories\Contract\PostRepositoryInterface;

class PostController extends Controller
{
    public $postRepository;

    public function __construct()
    {
        $this->postRepository = app(PostRepositoryInterface::class);
    }

    public function index()
    {
        return $this->postRepository->all();    
    }
}

或者,如果您想使用注入,请将App\Providers\RepositoryServiceProvider::class添加到config/app.php中的provider数组中。

PostController.php

<?php

namespace App\Http\Controllers;

use App\Repositories\Contract\PostRepositoryInterface;

class ReportController extends Controller
{
    public $postRepository;

    public function __construct(PostRepositoryInterface $postRepository)
    {
        $this->postRepository = $postRepository;
    }

    public function index()
    {
        return $this->postRepository->all();    
    }
}