定制 munguia-er/laravel-clean-generator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

munguia-er/laravel-clean-generator

Composer 安装命令:

composer require munguia-er/laravel-clean-generator

包简介

Generates Clean Architecture structure for Laravel — DTOs, Repositories, Services, Controllers with Web & API stacks

README 文档

README

Latest Version on Packagist PHP Version License Tests

A Laravel Artisan package that scaffolds a Clean Architecture (or Simple App-based) structure from a single command. It introspects your database schema and generates fully typed DTOs, Repository interfaces & implementations, Service interfaces & implementations, and Controllers — organized by architecture and stack (Web or API).

Features

  • 🏗 Dual architecture supportclean (Domain/Application/Infrastructure layers) or simple (flat App-based)
  • 🌐 Web & API controllers — configurable default stack, HTML or JSON responses
  • 🛡 Smart Form Requests — auto-generates StoreRequest and UpdateRequest with schema-mapped validation arrays (unique rules, foreign keys, limits, enums)
  • 🗂 Module sub-pathsBlog/Post generates App/DTOs/Blog/PostData.php, not App/Blog/Post/DTOs
  • 🎯 Explicit Primary Keys — extracts primary key type and auto-increment status dynamically from the database
  • 🔐 Strict validation — stops early if the model is missing and --table is not provided
  • 🔄 Model preservation — never moves or overwrites an existing model
  • 📦 Auto-binding — automatically registers Repository and Service bindings in AppServiceProvider
  • 🛣 Auto-routing — injects Route::resource() or Route::apiResource() into the correct route file
  • 🧬 Schema introspection — detects SoftDeletes, primary keys, nullable columns, casts
  • Full test coverage — runs against Orchestra Testbench

Requirements

Dependency Version
PHP ^8.1
Laravel 10.x, 11.x
nikic/php-parser ^5.0
doctrine/dbal ^3.8 | ^4.0

Installation

composer require munguia-er/laravel-clean-generator

The service provider is registered automatically via Laravel's package discovery.

Publish the configuration file:

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

Configuration

config/clean-generator.php

return [
    /*
     | Active architecture: 'clean' (default) or 'simple'.
     */
    'architecture'    => 'clean',

    /*
     | Default controller stack when neither --api nor --web is passed.
     | Options: 'web' | 'api'
     */
    'default_stack'   => 'web',

    'base_namespace'  => 'App',
    'base_path'       => null, // defaults to app_path()

    /*
     | Auto-register generated bindings and routes, respectively.
     */
    'auto_register'   => [
        'bindings' => true,
        'routes'   => true,
    ],

    /*
     | Directory paths for each artifact type, keyed by architecture.
     */
    'architectures' => [
        'simple' => [
            'models'                 => 'Models',
            'dtos'                   => 'DTOs',
            'requests'               => 'Http/Requests',
            'repository_interfaces'  => 'Interfaces/Repositories',
            'repositories'           => 'Repositories',
            'service_interfaces'     => 'Interfaces/Services',
            'services'               => 'Services',
            'controllers'            => 'Http/Controllers',
        ],
        'clean' => [
            'models'                 => 'Models',
            'dtos'                   => 'Domain/DTOs',
            'requests'               => 'Application/Http/Requests',
            'repository_interfaces'  => 'Domain/Contracts',
            'repositories'           => 'Infrastructure/Repositories',
            'service_interfaces'     => 'Domain/Services',
            'services'               => 'Domain/Services',
            'controllers'            => 'Application/Http/Controllers',
        ],
    ],
];

Usage

Basic

# Infer table from model name (model must exist)
php artisan clean:generate Post

# Specify table explicitly (generates model too)
php artisan clean:generate Post --table=posts

# Module sub-paths
php artisan clean:generate Blog/Post --table=posts
php artisan clean:generate Blog/Admin/Post --table=posts

Web vs API

# Web controller (HTML hardcoded responses)
php artisan clean:generate Blog/Post --table=posts --web

# API controller (JSON responses) — all files placed under Api/ sub-path
php artisan clean:generate Blog/Post --table=posts --api

# Using both flags is an error
php artisan clean:generate Blog/Post --table=posts --api --web  # → ERROR

Note: When --api is used, an Api/ segment is automatically injected into every layer's sub-path. This keeps Web and API scaffolding cleanly separated when both coexist in the same project.

Options

Option Description
{model} Model name, optionally with module path (Blog/Post)
--table= Database table to introspect. Required when the model does not exist.
--api Generate an API (JSON) controller under Api/ sub-path
--web Generate a Web (HTML) controller
--force Overwrite existing files

Generated Structure

Simple architecture — php artisan clean:generate Blog/Post --table=posts --web

app/
├── Models/Blog/Post.php
├── DTOs/Blog/
│   ├── PostData.php
│   ├── CreatePostData.php
│   └── UpdatePostData.php
├── Http/
│   ├── Requests/Blog/
│   │   ├── StorePostRequest.php
│   │   └── UpdatePostRequest.php
│   └── Controllers/Blog/PostController.php
├── Interfaces/
│   ├── Repositories/Blog/PostRepositoryInterface.php
│   └── Services/Blog/PostServiceInterface.php
├── Repositories/Blog/EloquentPostRepository.php
└── Services/Blog/PostService.php

Clean architecture — php artisan clean:generate Blog/Post --table=posts --api

app/
├── Models/Api/Blog/Post.php
├── Domain/
│   ├── DTOs/Api/Blog/
│   │   ├── PostData.php
│   │   ├── CreatePostData.php
│   │   └── UpdatePostData.php
│   ├── Contracts/Api/Blog/PostRepositoryInterface.php
│   └── Services/Api/Blog/
│       ├── PostServiceInterface.php
│       └── PostService.php
├── Infrastructure/Repositories/Api/Blog/EloquentPostRepository.php
└── Application/Http/
    ├── Requests/Api/Blog/
    │   ├── StorePostRequest.php
    │   └── UpdatePostRequest.php
    └── Controllers/Api/Blog/ApiPostController.php

Architecture Principles

Layer responsibility

Layer Responsibility
Models Eloquent ORM models, SoftDeletes, casts
DTOs Typed, readonly value objects for data transfer
Form Requests HTTP request validation logic and data authorization
Repository Interfaces Persistence contract (domain layer)
Repository Implementations Eloquent concrete implementations
Service Interfaces Business logic contract
Service Implementations Orchestrates repository + business rules
Controllers HTTP entry point; delegates entirely to service layer

Model handling behavior

  • If --table is provided → model is always generated (at the Api/-prefixed path when --api)
  • If --table is not provided and the model exists → model is left untouched; all other layers are generated around it
  • If --table is not provided and the model does not exist → command fails immediately with a descriptive error

Versioning

This package follows Semantic Versioning:

  • MAJOR — breaking changes to the generated file structure or command API
  • MINOR — new flags, new generators, new architecture options
  • PATCH — bug fixes, documentation, internal refactors

Testing

composer test

The test suite uses Orchestra Testbench with an in-memory SQLite database.

Contributing

Please read CONTRIBUTING.md for details on the development workflow and how to submit pull requests.

Code of Conduct

This project adheres to the Contributor Covenant. By participating, you are expected to uphold this code.

License

MIT — see LICENSE for details.

munguia-er/laravel-clean-generator 适用场景与选型建议

munguia-er/laravel-clean-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-27