devkits/module-generator 问题修复 & 功能扩展

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

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

devkits/module-generator

最新稳定版本:v1.1.0

Composer 安装命令:

composer require devkits/module-generator

包简介

Generate controller, model, view and migration for a Laravel module in one command

README 文档

README

A powerful Laravel package to generate a complete module (Controller, Model, and Migration) with a single artisan command. Streamline your development workflow and maintain consistency across your application.

Installation

You can install the package via composer:

composer require devkits/module-generator

Auto-Discovery

This package supports Laravel's auto-discovery feature. After you install it, the service provider will be automatically registered, and you can start using the package right away without any additional configuration.

For Laravel versions before 5.5 (which is unlikely since this package requires Laravel 8.0+), you would need to manually add the service provider to your config/app.php file:

'providers' => [
    // Other service providers...
    DevKits\ModuleGenerator\ServiceProvider::class,
],

Basic Usage

php artisan make:module Blog

This simple command will create:

  • A Blog model in app/Models/
  • A BlogController in app/Http/Controllers/
  • A migration file for the blogs table
  • A factory file for the model

Advanced Usage

Field Generation

Generate a module with specific fields:

php artisan make:module Product --fields="name:string,price:decimal,description:text,in_stock:boolean"

This will create:

  • A Product model with name, price, description, and in_stock as fillable fields
  • A migration with the appropriate columns and types
  • A ProductController with CRUD methods
  • A factory with appropriate faker methods for each field type

Select/Dropdown Fields

Define fields that should be rendered as select/dropdown menus with predefined options:

php artisan make:module Employee --fields="name:string,email:string,hire_date:date" --selects="department:engineering,marketing,sales,hr;status:active,on_leave,terminated"

This will:

  • Add department and status as enum fields in the migration
  • Add constants in the model for the dropdown options
  • Add validation rules in the controller
  • Prepare the controller to pass options to views

The generated model will include constants for the select options:

class Employee extends Model
{
    // ...

    // Select options for fields
    public const DEPARTMENT_OPTIONS = ['engineering', 'marketing', 'sales', 'hr'];
    public const STATUS_OPTIONS = ['active', 'on_leave', 'terminated'];
}

API Controllers

Generate API-focused controllers that return JSON responses:

php artisan make:module User --api

Or:

php artisan make:module User --type=api

Generate Views

php artisan make:module Comment --views

This creates Blade views for:

  • Index (listing)
  • Create form
  • Edit form
  • Show/detail page

Module Directory Structure

Create modules in a dedicated directory structure:

php artisan make:module Order --module-style

This organizes the module in a modular structure:

Modules/
  ├── Order/
      ├── Http/
      │   └── Controllers/
      │       └── OrderController.php
      ├── Models/
      │   └── Order.php
      ├── Database/
      │   ├── Migrations/
      │   │   └── xxxx_xx_xx_xxxxxx_create_orders_table.php
      │   └── Factories/
      │       └── OrderFactory.php
      ├── Resources/
      │   └── views/
      │       ├── index.blade.php
      │       ├── create.blade.php
      │       ├── edit.blade.php
      │       └── show.blade.php
      ├── routes.php
      └── OrderServiceProvider.php

Example Scenarios

1. Basic CMS Article Module

php artisan make:module Article --fields="title:string,slug:string,content:text,excerpt:text,published_at:timestamp" --views

2. E-commerce Product Module

php artisan make:module Product --fields="name:string,slug:string,sku:string,price:decimal,cost:decimal,quantity:integer,description:text" --selects="status:draft,published,out_of_stock,discontinued;category:electronics,clothing,home,beauty" --views

3. User Management Module

php artisan make:module User --fields="name:string,email:string:unique,email_verified_at:timestamp,password:string,remember_token:string" --selects="role:admin,editor,member,guest" --type=api

4. Task Management Module

php artisan make:module Task --fields="title:string,description:text,due_date:date,completed_at:timestamp:nullable" --selects="priority:low,medium,high,urgent;status:backlog,todo,in_progress,review,completed" --module-style --views

Working with Generated Files

Models

The generated models will include fillable fields and constants for select options:

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
    use HasFactory;

    protected $fillable = ['title', 'description', 'due_date', 'completed_at', 'priority', 'status'];

    // Select options for fields
    public const PRIORITY_OPTIONS = ['low', 'medium', 'high', 'urgent'];
    public const STATUS_OPTIONS = ['backlog', 'todo', 'in_progress', 'review', 'completed'];
}

Controllers

The generated controllers include validation for select fields and are prepared to pass options to views:

public function create()
{
    // Select options for dropdown fields
    $priorityOptions = Task::PRIORITY_OPTIONS;
    $statusOptions = Task::STATUS_OPTIONS;

    return view('task.create', [
        'priorityOptions' => $priorityOptions,
        'statusOptions' => $statusOptions
    ]);
}

public function store(Request $request)
{
    $validated = $request->validate([
        'title' => 'required|string|max:255',
        'description' => 'nullable|string',
        'due_date' => 'nullable|date',
        'priority' => 'required|in:' . implode(',', Task::PRIORITY_OPTIONS),
        'status' => 'required|in:' . implode(',', Task::STATUS_OPTIONS),
    ]);

    Task::create($validated);

    return redirect()->route('task.index')
        ->with('success', 'Task created successfully.');
}

Views

When using the --views option, the package generates view files that include all necessary form fields, including select dropdowns for enum fields:

<div class="form-group mb-3">
    <label for="status">Status</label>
    <select class="form-control @error('status') is-invalid @enderror" id="status" name="status">
        <option value="">Select Status</option>
        @foreach($statusOptions as $option)
            <option value="{{ $option }}" {{ old('status', $task->status ?? '') == $option ? 'selected' : '' }}>
                {{ ucfirst(str_replace('_', ' ', $option)) }}
            </option>
        @endforeach
    </select>
    @error('status')
        <span class="invalid-feedback" role="alert">
            <strong>{{ $message }}</strong>
        </span>
    @enderror
</div>

Configuration

Publish the configuration file:

php artisan vendor:publish --tag=module-generator-config

This creates config/module-generator.php with options:

return [
    'path' => 'App',
    'generate_views' => false,
    'view_path' => 'resources/views',
    'default_type' => 'full',
    'add_routes' => true,
    'api_version' => 'v1',
];

The configuration options allow you to:

  • path: Define the base path for generated files (default: 'App')
  • generate_views: Automatically generate views for all modules (default: false)
  • view_path: Define where views will be created (default: 'resources/views')
  • default_type: Set the default module generation type (default: 'full')
  • add_routes: Automatically add routes to route files (default: true)
  • api_version: Define the API version prefix for API routes (default: 'v1')

You can customize these settings according to your project's needs. For example, if you always want to generate views, you could set generate_views to true in your configuration, eliminating the need to pass the --views flag each time.

Testing

composer test

Credits

Christopher Vistal

License

The MIT License (MIT). Please see License File for more information.

devkits/module-generator 适用场景与选型建议

devkits/module-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 03 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-20