khazhinov/laravel-lighty

一套快速创建CRUD REST API的工具

11.0.7 2024-03-18 17:47 UTC

README

Social Card of Laravel Lighty

Laravel Lighty ⚡️

一套用于快速创建CRUD REST API的工具

描述

该库提供了一套工具,用于快速创建执行基本CRUD操作的REST API。

安装

安装此库需要使用Composer(Laravel 9+)

composer require "khazhinov/laravel-lighty:^9.0"

如有需要,请发布配置文件

php artisan vendor:publish --provider="Khazhinov\LaravelLighty\LaravelLightyServiceProvider" --tag="config"

以及XSLX导出模板

php artisan vendor:publish --provider="Khazhinov\LaravelLighty\LaravelLightyServiceProvider" --tag="views"

错误处理器

库提供了一个基本的错误处理器类,该类将根据接受的格式化结构对服务器响应进行格式化。

要实现错误处理器,请从Khazhinov\LaravelLighty\Exceptions\ExceptionHandler继承App\Exceptions\Handler类(app/Exception/Handler.php)

namespace App\Exceptions;

use Khazhinov\LaravelLighty\Exceptions\ExceptionHandler;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array<int, class-string<Throwable>>
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array<int, string>
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];
}

使用

该库提供了一套Artisan命令,用于快速生成创建REST API所需的所有类。

简单使用

php artisan lighty:generator TestEntity v1.0 --migration

执行此命令后,将生成以下文件结构

终端还将输出添加到路由器所需的信息

#/api/v1.0/testEntities
Route::group([
    "namespace" => "TestEntity",
    "prefix" => "/testEntities",
    "as" => "test_entities.",
], static function () {
    Route::get("/validations/{method?}", "TestEntityCRUDController@getValidations")->name("validations");

    Route::get("/", "TestEntityCRUDController@index")->name("index");
    Route::post("/search", "TestEntityCRUDController@index")->name("search");
    Route::post("/setPosition", "TestEntityCRUDController@setPosition")->name("set-position");

    Route::post("/", "TestEntityCRUDController@store")->name("store");
    Route::delete("/", "TestEntityCRUDController@bulkDestroy")->name("bulk-destroy");

    #/api/v1.0/testEntities/:key
    Route::group([
        "prefix" => "/{key}",
    ], static function () {
        Route::get("/", "TestEntityCRUDController@show")->name("show");
        Route::put("/", "TestEntityCRUDController@update")->name("update");
        Route::delete("/", "TestEntityCRUDController@destroy")->name("destroy");
    });
});

API路由器示例(routes/api.php)

<?php

use Illuminate\Support\Facades\Route;


# /api/v1.0/
Route::group(["namespace" => "App\Http\Controllers\Api\V1_0", "prefix" => "/v1.0", "as" => "api.v1_0"], static function () {
    #/api/v1.0/testEntities
    Route::group([
        "namespace" => "TestEntity",
        "prefix" => "/testEntities",
        "as" => "test_entities.",
    ], static function () {
        Route::get("/validations/{method?}", "TestEntityCRUDController@getValidations")->name("validations");

        Route::get("/", "TestEntityCRUDController@index")->name("index");
        Route::post("/search", "TestEntityCRUDController@index")->name("search");
        Route::post("/setPosition", "TestEntityCRUDController@setPosition")->name("set-position");

        Route::post("/", "TestEntityCRUDController@store")->name("store");
        Route::delete("/", "TestEntityCRUDController@bulkDestroy")->name("bulk-destroy");

        #/api/v1.0/testEntities/:key
        Route::group([
            "prefix" => "/{key}",
        ], static function () {
            Route::get("/", "TestEntityCRUDController@show")->name("show");
            Route::put("/", "TestEntityCRUDController@update")->name("update");
            Route::delete("/", "TestEntityCRUDController@destroy")->name("destroy");
        });
    });
});

许可证

MIT许可证。有关更多信息,请参阅许可证文本