bgpgroup/api-builder

dev-main 2022-07-22 01:26 UTC

This package is auto-updated.

Last update: 2024-09-22 06:12:07 UTC


README

Latest Version on Packagist Software License Build Status Scrutinizer Coverage Quality Score Total Downloads

API Builder 是一个 Laravel 扩展包,允许通过命令构建模块和资源。

使用此包,您可以构建

  • 模块
  • 资源
  • 集合
  • 请求
  • DTO
  • 控制器
  • 模型
  • 测试

您还可以使用模块和资源命令一次性创建所有这些

安装

要通过 Composer 安装,请运行以下命令

composer require bgpgroup/api-builder

该包将自动注册服务提供者和别名。

文档

模块

要构建一个新模块,请运行以下命令

php artisan bgp:make:module Sales

其中 销售 将是模块名称

此命令将创建

  • src/Modules/Sales/Providers/AppServiceProvider.php
  • src/Modules/Sales/Providers/AuthServiceProvider.php
  • src/Modules/Sales/config/sales.php
  • src/Modules/Sales/routes/api.php

集合

运行以下命令以构建一个集合

php artisan bgp:make:collection Order --module=Sales

其中 订单 将是资源名称,销售 将是模块名称

此命令将创建

  • src/Modules/Sales/Collections/OrderCollection.php

控制器

对于控制器,运行以下命令

php artisan bgp:make:controller Order --module=Sales

其中 订单 将是资源名称,销售 将是模块名称

此命令将创建

  • src/Modules/Sales/Controllers/OrderController.php

迁移

在运行命令之前,您必须设置表列

编辑 src/Modules/Sales/config/sales.php 添加 'resources' 键,如下所示

<?php

return [

    'resources' => require_once('builder.php'),

    'roles' => [

    ],
];

然后创建一个新 PHP 文件 src/Modules/Sales/config/builder.php,内容如下所示(示例)

<?php

return [
    'Order' => [

        'migration' => [
            'table' => 'orders',
            'columns' => [
                'id' => true,
                'name' => [
                    'type' => 'string',
                    'size' => '100',
                ],
                'description' => [
                    'type' => 'text',
                    'nullable' => true,
                ],
                'amount' => [
                    'type' => 'double',
                    'size' => '100',
                    'default' => '0'
                ],
                'date' => [
                    'type' => 'dateTime',
                ],
                'client_id' => [
                    'type' => 'uuid',
                    'nullable' => true,
                ],
                'income' => [
                    'type' => 'enum',
                    'options' => "['on', 'off']",
                    'default' => 'on'
                ],
                'expenses' => [
                    'type' => 'enum',
                    'options' => "['on', 'off']",
                    'default' => 'on'
                ],
                'active' => true,
                'created_by' => true,
                'company_id' => true,
                'timestamps' => true,
                'soft_delete' => true
            ],
        ]
    ],
];

此配置文件允许您使用命令在迁移文件中生成表列

php artisan bgp:make:migration Order --module=Sales

其中 订单 将是资源名称,销售 将是模块名称

此命令将创建

  • src/Modules/Sales/migrations/2022_06_20_212536_create_orders_table.php

使用前面的配置,这将生成以下代码

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
			$table->uuid('id')->primary();
			$table->string('name', 100);
			$table->text('description')->nullable();
			$table->double('amount');
			$table->dateTime('date');
			$table->uuid('client_id')->nullable();
			$table->enum('income', ['on', 'off'])->default('on');
			$table->enum('expenses', ['on', 'off'])->default('on');
			$table->enum('active',['on', 'off'])->default('on');
			$table->foreignUuid('created_by')->nullable()->constrained('users');
			$table->foreignUuid('company_id');
			$table->timestamps();
			$table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('orders');
    }
};

许可证

MIT 许可证(MIT)。请参阅 许可证文件 以获取更多信息。