luthfi/simple-crud-generator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

luthfi/simple-crud-generator

Composer 安装命令:

composer require --dev luthfi/simple-crud-generator

包简介

A simple CRUD generator for Laravel 5.5 (and later) with Bootstrap 5.

README 文档

README

Build Status Total Downloads

Need faster TDD in Laravel project? This is a simple CRUD generator complete with automated testing suite.

Documentation

For installation instructions and usage, please take a look at the official documentation.

About this package

This package contains artisan make:crud commands to create a simple CRUD feature with test classes on our Laravel 5.5 (and later) application. This package is fairly simple, to boost test-driven development method on our laravel application. It now supports Laravel 12 and 13.

With this package installed on local environment, we can use (e.g.) php artisan make:crud Vehicle command to generate some files :

  • App\Models\Vehicle.php eloquent model
  • xxx_create_vehicles_table.php migration file
  • VehicleController.php
  • index.blade.php and forms.blade.php view file in resources/views/vehicles directory
  • resources/lang/vehicle.php lang file
  • VehicleFactory.php model factory file
  • VehiclePolicy.php model policy file in app/Policies directory
  • ManageVehiclesTest.php feature test class in tests/Feature directory
  • VehicleTest.php unit test class in tests/Unit/Models directory
  • VehiclePolicyTest.php unit test class in tests/Unit/Policies directory

It will update some file :

  • Update routes/web.php to add vehicles resource route
  • Update app/providers/AuthServiceProvider.php to add Vehicle model Policy class in $policies property

It will also create this file if it not exists :

  • resources/lang/app.php lang file if it not exists
  • tests/BrowserKitTest.php base Feature TestCase class if it not exists

Main purpose

The main purpose of this package is for faster Test-driven Development, it generates model CRUD scaffolds complete with Testing Classes which will use Laravel Browserkit Testing package and PHPUnit.


How to install

For Laravel 10x and later

# Get the package
$ composer require luthfi/simple-crud-generator:^4.0 --dev

For Laravel 9.x

# Get the package
$ composer require luthfi/simple-crud-generator:^3.0 --dev

For Laravel 8.x

# Get the package
$ composer require luthfi/simple-crud-generator:^2.0 --dev

For Laravel 5.6 to 7.x

# Get the package
$ composer require luthfi/simple-crud-generator:^1.0 --dev

For Laravel 5.5

To use this package on laravel 5.5, we need to add the package (with browserkit) within require-dev in composer.json file, like so :

# Install required package for laravel/browser-kit-testing
$ composer require symfony/css-selector:^3.0

# Get the package
$ composer require luthfi/simple-crud-generator 1.2.* --dev

The package will auto-discovered.


How to use

Just type in terminal $ php artisan make:crud ModelName command, it will create simple Laravel CRUD files of given model name completed with tests.

For example we want to create CRUD for 'App\Models\Vehicle' model.

$ php artisan make:crud-simple Vehicle

Vehicle resource route generated on routes/web.php.
Vehicle model generated.
Vehicle table migration generated.
VehicleController generated.
Vehicle index view file generated.
Vehicle form view file generated.
lang/app.php generated.
vehicle lang files generated.
Vehicle model factory generated.
Vehicle model policy generated.
AuthServiceProvider class has been updated.
BrowserKitTest generated.
ManageVehiclesTest generated.
VehicleTest (model) generated.
VehiclePolicyTest (model policy) generated.
CRUD files generated successfully!

Make sure we have set database credential on .env file, then :

$ php artisan migrate
$ php artisan serve

Then visit our application url: http://localhost:8000/vehicles.


Usage on Fresh Install Laravel 8.x

In this example, we are using the laravel installer package to install new laravel project.

# This is example commands for Ubuntu users.
$ laravel new project-directory
$ cd project-directory
$ composer require laravel/ui
$ php artisan ui bootstrap --auth
$ npm install && npm run dev # Might need to run twice, minimum requirement: NodeJS v12.x
$ vim .env # Edit your .env file to update database configuration

# Install the package
$ composer require luthfi/simple-crud-generator:^2.0

# I really suggest "git commit" your project right before you run the make:crud command
$ php artisan make:crud Vehicle # Model name in singular

$ php artisan migrate
$ php artisan serve
# Visit your route http://127.0.0.1:8000
# Register as a new user
# Visit your route http://127.0.0.1:8000/vehicles

# Run the unit tests
$ vim phpunit.xml # Remove comments on the DB_CONNECTION and DB_DATABASE lines
$ vendor/bin/phpunit

Available Commands

# Create Full CRUD feature with tests
$ php artisan make:crud ModelName

# Create Full CRUD feature with tests and Bootstrap 3 views
$ php artisan make:crud ModelName --bs3

# Create Simple CRUD feature with tests
$ php artisan make:crud-simple ModelName

# Create Simple CRUD feature with tests and Bootstrap 3 views
$ php artisan make:crud-simple ModelName --bs3

# Create API CRUD feature with tests
$ php artisan make:crud-api ModelName

Model Attribute/column

The Model and table will only have 2 pre-definded attributes or columns : title and description on each generated model and database table. You can continue working on other column on the table.


Bootstrap 4 Views

The generated view files use Bootstrap 4 by default (for Laravel 5.6 and later).


Bootstrap 3 Views

We can also generates views that use Bootstrap 3 with --bs3 command option, eg for Laravel version 5.5.


For API

If we want to generate API Controller with feature tests, we use following command :

$ php artisan make:crud-api Vehicle

By default, we use Laravel Token Based Authentication, so we need to update our user model.

  1. Add api_token column on our users_table_migration.
  2. Add api_token as fillable property on User model.
  3. Add api_token field on our UserFactory.

API Usage

The generated API is a REST API, using GET and POST verbs, with a URI of /api/modelname.

Example code for calling the generated API, using Guzzle:

// Read data a specific Vehicle record...
$uri = 'http://your-domain.com/api/vehicles/'.$vehicleID;
$headers = ['Authorization' => 'Bearer '.$apiToken];

$client = new \GuzzleHttp\Client();
$res = $client->request('GET', $uri, ['headers' => $headers]);

// Create a new Vehicle record...
$uri = 'http://your-domain.com/api/vehicles';
$headers = ['Authorization' => 'Bearer '.$apiToken];
$payload = json_encode([
    'title' => 'Vehicle Name 1',
    'description' => 'Vehicle Description 1',
]);

$client = new \GuzzleHttp\Client();
$res = $client->request('POST', $uri, ['body' => $payload, 'headers' => $headers]);

The generated functional tests will give you examples of how to adapt this code for other call types.


Config file

You can configure your own by publishing the config file:

$ php artisan vendor:publish --provider="Luthfi\CrudGenerator\ServiceProvider" --tag=config

That will generate config/simple-crud.php file.

By default, this package have some configuration:

<?php

return [
    // The master view layout that generated views will extends
    'default_layout_view' => 'layouts.app',

    // The base test case class path for generated testing classes
    'base_test_path' => 'tests/BrowserKitTest.php',

    // The base test class full name
    'base_test_class' => 'Tests\BrowserKitTest',
];

Publishing Stub Files

Stub files is the templates which we use to generate the code for each model classes and files. We can customize the stub files as we needed by publishing them to our project directory.

$ php artisan vendor:publish --provider="Luthfi\CrudGenerator\ServiceProvider" --tag=stubs

That will generate stub files on stubs/simple-crud directory. Now we can change some stub files based on our project needs.


Attention

  • The package will creates the Model class file, the command will stop if the Model already exists.
  • You need a resources/views/layouts/app.blade.php view file, simply create one with php artisan make:auth command. You can change this configuration via the config/simple-crud.php file.

The --parent-model Command Option

We have --parent-model option to create a CRUD feature nested with existing model.

I often creating a CRUD feature nested with the existing model, like:

  • Adding payment history entries for an invoice
  • Adding work orders under a project
  • Adding attachments under document
  • Many more

Since this package was (only) for creating the "first-class citizen" CRUD feature, we need to create the nested CRUD feature manually. The command --parent-model option can save our time more.

Example

When one department has many section, then we are adding a nested controller like so:

Route::resource('departments.sections', Departements\SectionController::class);

So we will have routes like:

GET departments/{department}/sections
GET departments/{department}/sections/{section}
GET departments/{department}/sections/create
POST departments/{department}/sections
GET departments/{department}/sections/{section}
GET departments/{department}/sections/{section}/edit
PATCH departments/{department}/sections/{section}
DELETE departments/{department}/sections/{section}

Including the model, controller, views, feature test, unit tests.

To create the CRUD feature, we can run:

php artisan make:crud --parent-model=Department Section

We will have a working Section CRUD feature with title and description attribute under Departments namespace, completed with tests. (Where Department is the existing model and Section is the new model.)


Screenshots

Visit your application in new resource route : http://127.0.0.1:8000/vehicles

Generated CRUD page by Simple CRUD Generator


Generated testing suite

Next, let us try the generated testing suite. To use the generated testing classes, we can set the database environment using in-memory database SQLite. Open phpunit.xml. Add two lines below on the env :

<phpunit>
    <!-- ..... -->
    <php>
        <!-- ..... -->
        <server name="DB_CONNECTION" value="sqlite"/>
        <server name="DB_DATABASE" value=":memory:"/>
    </php>
</phpunit>

Then run PHPUnit

$ vendor/bin/phpunit

All tests should be passed.

Generated Testing Suite on Simple CRUD Generator


Issue/Proposal

If you find any issue, or want to propose some idea to help this package better, please create an issue in this github repo.


License

This package is open-sourced software licensed under the MIT license.

luthfi/simple-crud-generator 适用场景与选型建议

luthfi/simple-crud-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.17k 次下载、GitHub Stars 达 171, 最近一次更新时间为 2017 年 11 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「testing」 「crud」 「laravel-package」 「crud-generator」 「artisan-command」 「tdd-workflow」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 luthfi/simple-crud-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 luthfi/simple-crud-generator 我们能提供哪些服务?
定制开发 / 二次开发

基于 luthfi/simple-crud-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 26.17k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 171
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 171
  • Watchers: 7
  • Forks: 44
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-10