cuongnd88/lara-repository

自动生成的接口和仓库文件

1.2 2021-06-18 08:31 UTC

This package is auto-updated.

Last update: 2024-09-22 17:37:08 UTC


README

仓库是在领域和持久层之间的一种分离。仓库提供了一个集合接口,用于访问存储在数据库、文件系统或外部服务中的数据。数据以对象的形式返回。

在 Laravel 应用程序中使用 Repository 模式 的主要思想是在模型和控制器之间创建一个桥梁。这种模式使您的 Laravel 代码更干净、更安全,值得使用仓库来分离模型不应负责的责任。

此包可以帮助自动生成接口、仓库、模型和控制器文件,节省时间并支持您专注于实现逻辑。 特别是,您不需要在 Service provider 类中绑定接口和仓库类

1-使用 Composer 安装 cuongnd88/lara-repository

$ composer require cuongnd88/lara-repository

2-在 config/app.php 中添加以下服务提供者

<?php
// config/app.php
return [
    // ...
    'providers' => [
        // ...
        Cuongnd88\LaraRepo\LaraRepoServiceProvider::class,
    ]
    // ...
];

3-运行 make:repository 命令

php artisan make:repository --interface=User/UserInterface --repository=User/UserRepository --model=Models/User --controller=User/UserController

--interface 用于指示接口文件。

--repository 用于指示仓库文件。

--model 用于分配模型。如果不存在,它将确认创建一个模型。

--controller 选项用于创建控制器文件。使用 @resource,它生成一个资源控制器类。

示例用法

通过运行命令开始 code less

php artisan make:repository --interface=Staff/StaffInterface --repository=Staff/StaffRepository --model=Models/Staff --controller=Staff/StaffController@resource

运行命令时将创建 Repositories 目录。所有接口和仓库文件都存储在 app/Repositories/Language 目录中。

现在在 StaffController 中,StaffInterface 已注入到 construct 方法中。这个动作被称为 依赖注入

<?php

namespace App\Http\Controllers\Staff;

use App\Http\Controllers\Controller;
use App\Repositories\Staff\StaffInterface;
use Illuminate\Http\Request;

class StaffController extends Controller
{
    protected $staffInterface;

    public function __construct(StaffInterface $staffInterface)
    {
        $this->staffInterface = $staffInterface;        
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

请查看配置目录中的自动生成的 repositories.php

<?php
//config/repositories.php
return [
    \App\Repositories\User\UserInterface::class => \App\Repositories\User\UserRepository::class,
    \App\Repositories\Staff\StaffInterface::class => \App\Repositories\Staff\StaffRepository::class,
    \App\Repositories\Language\LanguageInterface::class => \App\Repositories\Language\LanguageRepository::class,

];

Repository 模式还允许我们在控制器内部编写更少的代码,这使得它比拥有巨大的代码的控制器更好,这不是我们想要的,如果我们目标是更好的可维护性和可读性。让我们保持这种方式,保持控制器干净。

演示

这是演示源代码

app/Repositories

Staff/StaffController.php