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

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

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

nirajkhadka/laravel-module-generator

Composer 安装命令:

composer require nirajkhadka/laravel-module-generator

包简介

A Laravel package that generates complete CRUD modules with controllers, services, actions, DTOs, form requests, models, and migrations using artisan commands.

README 文档

README

A Laravel package that quickly generates complete CRUD modules including Controllers, Services, Actions, DTOs, Form Requests, Models, Migrations, and Resources via a single Artisan command.

Why Use This Package?

Creating CRUD modules repeatedly can be tedious. This package scaffolds all the essential files you need to get started with clean, maintainable, and consistent code architecture — so you can focus on business logic instead of boilerplate.

Features

  • Generate Controllers, Services, and Action classes for CRUD operations
  • Generate Data Transfer Objects (DTOs) for data handling
  • Generate Form Request classes with validation
  • Generate Eloquent Models with UUID support
  • Generate timestamped database migration files
  • Generate API Resource classes for consistent JSON responses
  • Support for nested modules with proper namespaces
  • Configurable base namespace and paths
  • Stub files can be published and customized

Installation

Require the package via Composer:

composer require nirajkhadka/laravel-module-generator --dev

Publish configuration and stubs:

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

Usage

Generate a new module with:

php artisan module:make {ModuleName}

Example:

php artisan module:make Customer

This command creates:

  • Controller (CustomerController.php)
  • Service (CustomerService.php)
  • Actions: Index, Store, Update, Delete
  • DTO (CustomerDto.php)
  • Requests: Index, Store, Update
  • Model (Customer.php)
  • Migration file for the table
  • API Resource (CustomerResource.php)

Nested Modules

You can specify nested namespaces by using slashes:

php artisan module:make Admin/Customer

This generates the module under App\Http\Controllers\Admin, App\Services\Admin, etc.

Configuration

Modify the published config file config/module-generation-module.php to customize:

  • Base namespace
  • Paths for controllers, services, actions, DTOs, and requests

Customizing Stubs

Customize generated files by modifying the stub files located at:

stubs/vendor/module-generator-stubs

Publish stubs to your project by running the vendor publish command (shown above).

Contributing

Feel free to open issues or submit pull requests on the GitHub repo.

License

MIT License © Niraj Khadka

Author

Niraj Khadka
Email: khadka.niraj11111@gmail.com
GitHub: Nirajkhad

Usage Examples

Basic Module Generation

Generate a simple module named Product:

php artisan module:make Product

This creates:

  • app/Http/Controllers/ProductController.php
  • app/Services/ProductService.php
  • Actions: IndexAction.php, StoreAction.php, UpdateAction.php, DeleteAction.php
  • DTO: ProductDto.php
  • Requests: IndexRequest.php, StoreRequest.php, UpdateRequest.php
  • Model: Product.php
  • Migration: timestamped create_products_table.php
  • API Resource: ProductResource.php

Nested Module with Namespace

Generate a nested module Admin/User with namespacing:

php artisan module:make Admin/User

Creates:

  • app/Http/Controllers/Admin/UserController.php
  • app/Services/Admin/UserService.php
  • Actions inside app/Actions/Admin/User/
  • Requests inside app/Http/Requests/Admin/User/
  • Model: app/Models/Admin/User.php (if your config supports nested models)
  • Migration and resources properly namespaced

This keeps code organized in subfolders and namespaces.

Customizing Namespace and Paths

Publish the config file:

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

Then edit config/module-generation-module.php:

return [
    'base_namespace' => 'App',
    'paths' => [
        'controllers' => 'Http/Controllers/Custom',
        'services' => 'Domain/Services',
        'actions' => 'Domain/Actions',
        'dtos' => 'Domain/Dtos',
        'requests' => 'Http/Requests/Custom',
    ],
];

Now when you run module:make Product, files will generate inside your custom directories.

FAQ

Q: Can I generate only specific parts of the module?
A: Currently, the package generates the full CRUD module at once. Selective generation may be planned for future versions.

Q: How do I override stub templates?
A: Run:

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

This publishes stub files to stubs/vendor/module-generator-stubs/. Modify these .stub files to customize generated code templates.

Q: Will this work with Laravel versions below 9?
A: No, it requires Laravel 9 or higher due to dependencies and PHP 8.1+ features.

Q: How do I change UUID generation or disable it?
A: Modify the model stub in your published stubs directory. You can change or remove the HasUuids trait and UUID logic as needed.

Q: How to add additional fields to the migration?
A: Edit the migration stub after publishing, or manually add columns after generation.

Troubleshooting Tips

  • Command Not Found: Ensure package is installed via Composer and ModuleServiceProvider is registered (auto-discovered by default).

  • Stubs Not Publishing: Check write permissions on your stubs/ directory and run the publish command again.

  • Namespace Issues: Verify the base_namespace and paths in the config file match your Laravel app structure.

  • Migration Timestamp Conflicts: If migration with the same name exists, you will be prompted to overwrite or skip.

  • Model Not Found in Controller: Check your namespace configurations, especially if you use nested modules.

Code Snippets for Extending or Modifying Generated Modules

Adding a New Method to the Service

Open the generated service, e.g., app/Services/ProductService.php and add:

public function getByName(string $name): ?Product
{
    return Product::where('name', $name)->first();
}

Adding Custom Validation to Requests

Modify StoreRequest.php:

public function rules(): array
{
    return [
        'name' => ['required', 'string', 'min:3', 'unique:products,name'],
        'price' => ['required', 'numeric', 'min:0'],
    ];
}

Add new fields accordingly in DTO and migration stubs as well.

Extending Controller with a Custom Endpoint

In ProductController.php, add:

public function search(Request $request)
{
    $name = $request->input('name');

    $product = $this->productService->getByName($name);

    if (!$product) {
        return response()->json(['message' => 'Product not found'], 404);
    }

    return response()->json(['data' => ProductResource::make($product)]);
}

Add a route to routes/api.php:

Route::get('products/search', [ProductController::class, 'search']);

Customizing Stub Files

After publishing stubs, edit any .stub file inside stubs/vendor/module-generator-stubs/ such as controller.stub:

Replace placeholders or add custom traits, imports, or methods that fit your coding style or project standards.

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-21