一个功能强大、有些人认为神奇的Laravel CRUD生成器

v1.7 2021-08-17 15:17 UTC

This package is auto-updated.

Last update: 2024-09-17 22:07:14 UTC


README

LCrud - 一个功能强大、有些人认为神奇的Laravel CRUD生成器

Latest Version Build Status Packagist license

它能够快速生成神奇的CRUD原型,并为你准备完整的测试脚本,需要编辑的内容非常少。遵循SOLID原则,它可以根据CLI中提供的表名构建一组基本的组件。CRUD可以用于单表实体,例如“books”或“authors”,但你也可以构建用于组合实体的CRUD,例如父级和子级结构:“books_authors”。这将生成一个“books_authors”表,并将作者的所有组件(控制器、服务、模型等)放入Books命名空间中,这意味着你可以生成“books_publishers”,并将所有组件作为作者的兄弟添加。现在假设你已经使用了Laracogs启动器套件,那么你可以使用它们启动构建CRUD,这意味着它们已经作为仪表板内容的视图扩展封装好了,这意味着你离完成应用程序又近了一步。

作者

要求

  1. PHP 7+
  2. OpenSSL

兼容性和支持

文档

php artisan lcrud:new {name or snake_names}
{--api}
{--ui=bootstrap}
{--serviceOnly}
{--withBaseService}
{--withFacade}
{--withoutViews}
{--migration}
{--schema}
{--relationships}

配置

配置发布在config中,你可以在此指定命名空间、位置等。

模板

所有生成的组件都基于模板。此包中包含基本模板,但在大多数情况下,你必须根据项目需求进行调整。如果你在安装过程中发布了资产,模板文件将可在resources/lcrud/crud中找到。

测试模板与其他模板的处理方式不同。默认情况下,提供了两个测试模板,一个用于生成的服务集成的测试,另一个用于验收测试。然而,测试文件夹与项目测试文件夹有一对一的映射。这意味着你可以轻松地为不同的测试级别添加测试,以匹配你的项目。例如,如果你想为生成的控制器创建单元测试,你只需创建一个新的模板文件,例如resources/lcrud/crud/Tests/Unit/ControllerTest.txt。在运行CRUD生成器时,将创建以下文件:tests/unit/NameOfResourceControllerTest.php

API

API选项将在控制器中添加处理API请求和响应,并添加API路由(假设为v1)。

yourapp.com/api/v1/books

UI

仅支持一个CSS框架(Bootstrap)。如果没有指定UI选项,LCrud将使用纯HTML视图,这看起来不太美观。

两者都期望一个仪表板父视图,可以通过以下命令生成

--ui=bootstrap

这些命令将使用CSS框架重新设计视图。

仅服务

仅服务选项将允许你生成服务层和更低层的CRUD,这包括:服务、模型和测试(带有迁移选项)。它将跳过控制器、路由、视图等。这可以使你的代码更简洁,对于在网站/应用程序中不具有“视觉”存在的实体关系(如实体的下载)来说,这是最优的。

带基本服务

如果你选择此选项,LCrud将为CRUD生成一个基本服务,该服务将从该CRUD的服务扩展。这很有用,当你想要减少代码重复时。

带外观

如果您选择启用外观(Facades),CRUD 将自动生成它们,目的是使用它们来访问服务。您需要在自己的提供者中将它们绑定到应用程序上,但至少会生成外观文件。

迁移

迁移选项将迁移文件添加到您的迁移目录,使用模式构建器将填充创建表方法。模式及其关系需要这一点,因为它们期望修改生成的迁移文件。

模式

需要迁移选项

您可以使用以下结构定义表模式。字段类型应与模式构建器匹配。

--schema="id:increments,name:string"

以下列出了可用的列类型

  • id
  • bigIncrements
  • increments
  • bigInteger
  • binary
  • boolean
  • char
  • date
  • dateTime
  • decimal
  • double
  • enum
  • float
  • integer
  • ipAddress
  • json
  • jsonb
  • longText
  • macAddress
  • mediumInteger
  • mediumText
  • morphs
  • smallInteger
  • string
  • text
  • time
  • tinyInteger
  • timestamp
  • uuid

还需要进一步的定义吗?

--schema="id:increments|first,user_id:integer|unsigned,name:string|nullable|after('id'),age:integer|default(0)"

您甚至可以处理一些参数,例如

--schema="id:increments|first,user_id:integer|unsigned,name:string(45)"

关系

需要迁移选项

您可以指定关系,以自动执行构建 CRUD 的更多步骤。您可以设置如下关系表达式

relation|class|column

或类似的东西

hasOne|App\Author|author

这将在模型中添加关系,以及添加所需的名字_id 字段到您的表中。还有一件事您不用担心。由 HTML 渲染器处理的通用关系包括

hasOne
hasMany
belongsTo

!!! 警告 "CRUD 当前不支持 belongsToMany,也就是说它目前不会创建关系表"

示例

以下组件将被生成

生成的文件

  • 控制器
  • API 控制器(可选)
  • 服务
  • 创建请求
  • 更新请求
  • 模型
  • 外观(可选)
  • 视图(Bootstrap 或 Semantic 或无 CSS 框架)
  • 测试
  • 迁移(可选)附加到以下文件
  • app/Http/routes.php
  • database/factories/ModelFactory.php

单词示例(书籍)

php artisan lcrud:new Book
--migration
--schema="id:increments,title:string,author:string"

当使用组件的默认路径时,以下文件将被生成

  • app/Http/Controllers/BookController.php
  • app/Http/Requests/BookCreateRequest.php
  • app/Http/Requests/BookUpdateRequest.php
  • app/Models/Book/Book.php
  • app/Services/BookService.php
  • resources/views/book/create.blade.php
  • resources/views/book/edit.blade.php
  • resources/views/book/index.blade.php
  • resources/views/book/show.blade.php
  • database/migrations/create_books_table.php
  • tests/BookIntegrationTest.php
  • tests/BookServiceTest.php

蛇形命名示例(Book_Author)

php artisan lcrud:new Book_Author
--migration
--schema="id:increments,firstname:string,lastname:string"
--withFacade

当使用组件的默认路径时,以下文件将被生成

  • app/Facades/Books/AuthorServiceFacade.php
  • app/Http/Controllers/Books/AuthorController.php
  • app/Http/Requests/Books/AuthorCreateRequest.php
  • app/Http/Requests/Books/AuthorUpdateRequest.php
  • app/Models/Books/Author/Author.php
  • app/Services/Books/AuthorService.php
  • resources/views/book/author/create.blade.php
  • resources/views/book/author/edit.blade.php
  • resources/views/book/author/index.blade.php
  • resources/views/book/author/show.blade.php
  • database/migrations/create_book_authors_table.php
  • tests/Books/AuthorIntegrationTest.php
  • tests/Books/AuthorServiceTest.php

单名词示例(带有 API 的 Book)

php artisan lcrud:new Book
--api
--migration
--schema="id:increments,title:string,author:string"

当使用组件的默认路径时,以下文件将被生成

  • app/Http/Controllers/Api/BookController.php
  • app/Http/Controllers/BookController.php
  • app/Http/Requests/BookCreateRequest.php
  • app/Http/Requests/BookUpdateRequest.php
  • app/Models/Book/Book.php
  • app/Services/BookService.php
  • resources/views/book/create.blade.php
  • resources/views/book/edit.blade.php
  • resources/views/book/index.blade.php
  • resources/views/book/show.blade.php
  • database/migrations/create_books_table.php
  • tests/BookIntegrationTest.php
  • tests/BookServiceTest.php

这是 CRUD 构建器生成的示例。它已设置所有基本 CRUD 方法。

表格 CRUD

表格 CRUD 是 CRUD 的包装器,它将解析数据库中的表格并从该表格构建 CRUD。

您必须确保名称与表名称的大小写匹配

php artisan lcrud:table {name or snake_names}
{--api}
{--ui=bootstrap}
{--serviceOnly}
{--withFacade}
{--relationships}

LForm

LForm - Laravel 的一个令人惊叹的神奇表单和输入制作工具。

LForm包提供了一套工具,只需一行代码即可生成HTML表单。我们也不想编写枯燥的HTML,LForm将生成错误容器,所有由表或对象列类型定义的字段,或者如果您想有更多控制权,可以定义一个配置。

是时候发布这些资产了!

php artisan vendor:publish --provider="Luisobrinho\LForm\LFormProvider"

LForm指南

Blade指令

@form_maker_table();
@form_maker_object();
@form_maker_array();
@form_maker_columns();

辅助函数

form_maker_table();
form_maker_object();
form_maker_array();
form_maker_columns();

外观

LForm::fromTable();
LForm::fromObject();
LForm::fromArray();
LForm::getTableColumns();

常用组件

简单字段

这些组件是最简单的

class: 'a css class'
reformatted: true|false // Reformats the column name to remove underscores etc
populated: true|false // Fills in the form with values
idAndTimestamps: true|false // ignores the id and timestamp columns

列是一个具有以下性质的数组,可以用在任何fromX方法中替换列组件

[
    'id' => [
        'type' => 'hidden',
    ],
    'name' => [
        'type' => '', // defaults to standard text input
        'placeholder' => 'User Name Goes Here!',
        'alt_name' => 'User Name',
        'custom' => 'custom DOM attributes etc',
        'class' => 'css class names',
        'before' => '<span class="input-group-addon" id="basic-addon1">@</span>',
        'after' => '<span class="input-group-addon" id="basic-addon2">@example.com</span>',
    ],
    'job' => [
        'type' => 'select',
        'alt_name' => 'Your Job',
        'custom' => 'multiple', // custom attributes like multiple, disabled etc
        'options' => [
            'key 1' => 'value_1',
            'key 2' => 'value_2',
        ]
    ],
    'roles' => [
        'type' => 'relationship',
        'class' => 'App\Repositories\Roles\Roles',
        'label' => 'name' // the field for the label in the select input generated
    ]
]

列配置中支持的类型

  • 文本(转换为textarea)
  • 密码
  • 复选框
  • 内联复选框
  • 单选按钮
  • 选择框
  • 隐藏
  • 数字
  • float
  • decimal

** 如果没有设置类型,LForm将默认为标准文本输入

以下名称的列将默认不显示:id,created_at,updated_at。您需要在创建表单时覆盖此设置。

视图

您可以为LForm创建一个自定义视图:这是一个示例

<div class="row">
    <div class="form-group {{ $errorHighlight }}">
        <label class="control-label" for="{!! $labelFor !!}">{!! $label !!}</label>
        <div class="row">
            {!! $input !!}
        </div>
    </div>
    {!! $errorMessage !!}
</div>

fromTable()

LForm::fromTable($table, $columns = null, $class = 'form-control', $view = null, $reformatted = true, $populated = false, $idAndTimestamps = false)

示例

LForm::fromTable('users')

fromTable方法将遍历指定的表,并根据列和列类型构建表单。您可以自由定制它(见下文),上述基本示例将生成

<div class="form-group ">
    <label class="control-label" for="Name">Name</label>
    <input  id="Name" class="form-control" type="" name="name" placeholder="Name">
</div>
<div class="form-group ">
    <label class="control-label" for="Email">Email</label>
    <input  id="Email" class="form-control" type="" name="email" placeholder="Email">
</div>
<div class="form-group ">
    <label class="control-label" for="Password">Password</label>
    <input  id="Password" class="form-control" type="" name="password" placeholder="Password">
</div>
<div class="form-group ">
    <label class="control-label" for="Remember_token">Remember Token</label>
    <input  id="Remember_token" class="form-control" type="" name="remember_token" placeholder="Remember Token">
</div>

fromObject()

与上述相同的规则,我们可以而不是提供表字符串,我们可以插入一个对象,如Auth::user()或从数据库中检索的任何单个对象。

fromObject($object, $columns = null, $view = null, $class = 'form-control', $populated = true, $reformatted = false, $idAndTimestamps = false)

fromArray()

fromArray在上下文上与fromTable和fromObject相同,在这种情况下,我们能够提供属性的一个简单数组列表。与fromArray的区别在于,我们可以以两种格式之一提供这些

[ 'name', 'birthday' ]

[ 'name' => 'string', 'birthday' => 'date' ]

兼容的所有字段类型列表如下

  • integer
  • string
  • 日期时间
  • date
  • float
  • binary
  • 二进制大对象
  • boolean
  • 日期时间tz
  • time
  • 数组
  • json_array
  • 对象
  • decimal
  • 大整数
  • 小整数
fromArray($array, $columns = null, $view = null, $class = 'form-control', $populated = true, $reformatted = false, $idAndTimestamps = false)

getTableColumns()

getTableColumns方法利用Doctrines Dbal组件来映射您的数据库表并提供列和类型。这对于从对象创建编辑器表单的初始构建非常完美。

示例

LForm::fromObject(Books::find(1), LForm::getFromColumns('books'))

这将根据表的列构建表单。尽管fromObject会扫描对象,但提供表列作为列输入允许将输入设置为正确的类型。


----

# InputMaker Guide

The nice part about the input maker is that its the core of the form maker only pulled out. So this way you can reduce your HTML writing significanly with its blade directives or helpers.

## Blade Directives

```blade
@input_maker_label()
@input_maker_create()

辅助函数

input_maker_label();
input_maker_create();

外观

InputMaker::label();
InputMaker::create();

常用组件

为一切提供简单字段!

标签生成器是最简单的

input_maker_label('name', ['class' => 'something'])

输入生成器有更多部分

input_maker_create($name, $field, $object = null, $class = 'form-control', $reformatted = false, $populated = true)

$field参数是一个可以高度配置的数组。

示例 $feild 配置

[
    'type' => '', // defaults to standard text input
    'placeholder' => 'User Name Goes Here!',
    'alt_name' => 'User Name',
    'custom' => 'custom DOM attributes etc',
    'class' => 'css class names',
    'before' => '<span class="input-group-addon" id="basic-addon1">@</span>',
    'after' => '<span class="input-group-addon" id="basic-addon2">@example.com</span>',
]

对于关系

[
    'model' => 'Full class as string',
    'label' => 'visible name for the options',
    'value' => 'value for the options',
]

// Example without User:
@input_maker_create('roles', ['type' => 'relationship', 'model' => 'App\Repositories\Role\Role', 'label' => 'label', 'value' => 'name'])

// Example with User:
@input_maker_create('roles', ['type' => 'relationship', 'model' => 'App\Repositories\Role\Role', 'label' => 'label', 'value' => 'name'], $user)

配置中支持的类型

  • string
  • 文本(转换为textarea)
  • 密码
  • 复选框
  • 内联复选框
  • 单选按钮
  • 选择框
  • 隐藏
  • 数字
  • float
  • decimal
  • 关系

** 如果没有设置类型,InputMaker将默认为标准文本输入

许可证

LCrud是开源软件,许可协议为MIT许可证

错误报告和功能请求

请在提交问题和功能请求时添加尽可能多的细节

免责声明

软件按“原样”提供,不提供任何形式的保证,无论是明示的、暗示的,包括但不限于适销性、特定用途的适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论是基于合同、侵权或其他方式,由此软件或软件的使用或其他交易而产生。