vladmunj/crud-generator

为Lumen提供的CRUD生成器

v1.9.1 2024-06-24 10:39 UTC

README

Lumen包,用于生成CRUD控制器、模型和路由

安装

运行以下命令

composer require vladmunj/crud-generator

环境变量

安装包后,更改数据库连接设置,并将SWAGGER_VERSION、PACKAGE_AUTHOR变量放入您的.env文件中

DB_CONNECTION=YOUR_DB_TYPE[例如 mysql,pgsql]
DB_HOST=DATABASE_HOST
DB_PORT=DATABASE_PORT
DB_DATABASE=DATABASE_NAME
DB_USERNAME=DATABASE_USERNAME
DB_PASSWORD=DATABASE_PASSWORD

SWAGGER_VERSION=3.0
PACKAGE_AUTHOR=AUTHOR_NAME

配置

将CrudGeneratorProvider添加到bootstrap/app.php中的providers部分

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/
// $app->register(App\Providers\AppServiceProvider::class);
// $app->register(App\Providers\AuthServiceProvider::class);
// $app->register(App\Providers\EventServiceProvider::class);
$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class);
$app->register(Vladmunj\CrudGenerator\CrudGeneratorServiceProvider::class);

在您的bootstrap/app.php中取消注释 $app->withEloquent() 和 $app->withFacades() 调用

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| Here we will load the environment and create the application instance
| that serves as the central piece of this framework. We'll use this
| application as an "IoC" container and router for this framework.
|
*/

$app = new Laravel\Lumen\Application(
    dirname(__DIR__)
);

$app->withFacades();

$app->withEloquent();

运行包迁移

php artisan migrate

用法

在使用命令之前,您需要创建并运行迁移,为CRUD操作创建表,例如

php artisan make:migration create_tests_table
php artisan migrate

命令

php artisan make:crud

命令将询问您进行CRUD所需的参数

控制器名称
>
CRUD URL
>
模型名称
>
表名称
>

  • 控制器名称:CRUD操作的控制器的名称。
  • CRUD URL:CRUD操作的路由。例如,值 api/test 将生成类似这样的路由
/**
* Controller routes
*/
$router->group(["prefix"=>"api/test"],function() use($router){
    // CRUD
    $router->post("/","TestController@create");
    $router->get("/","TestController@all");
    $router->get("/{id}","TestController@get");
    $router->put("/{id}","TestController@update");
    $router->delete("/{id}","TestController@delete");
});
  • 模型名称:CRUD操作的模型的名称。
  • 表名称:CRUD操作的表的名称。

您可以使用命令检查新路由

php artisan route:list

附加命令

php artisan crud:route

通过id删除CRUD路由组,或设置id = 0时删除所有路由组

php artisan make:crud:table

为您的所有表生成CRUD。您可以设置将排除在生成之外的表名。默认将排除的表名:'users'、'crud_route_groups'、'migrations'。