lucasandre-dev / laravel-code-generator
从模板生成代码
2.0.2
2024-08-15 14:54 UTC
README
为 Laravel 项目提供一个简单的代码生成器,以简化开发过程。
🚀 安装和配置
-
通过 Composer 安装
composer require lucasandre-dev/laravel-code-generator
-
发布服务提供者 在你的
config/app.php
文件中注册服务提供者'providers' => [ // Other Service Providers... LucasandreDev\LaravelCodeGenerator\Providers\CodeGeneratorProvider::class, ],
注意
对于 Laravel 版本 <= 10: 在 Laravel 10 中注册提供者
对于 Laravel 版本 >= 11: 在 Laravel 11 中注册提供者 -
发布配置和草图
- 配置文件
php artisan vendor:publish --tag=templates-code-generator-config
- 草图
php artisan vendor:publish --tag=templates-code-generator-mockups
- 配置文件
-
添加文件系统配置 将以下配置添加到
config/filesystems.php
'templates' => [ 'driver' => 'local', 'root' => base_path(), 'throw' => true, ],
-
配置模板生成器 修改
config/templates-code-generator.php
中的配置以满足你的需求。 -
运行代码生成命令 执行以下命令生成代码
php artisan code:generate
注意
运行命令时,系统会提示你提供实体名称和模板名称。
📚 文档
🔧 配置文件 (config/templates-code-generator.php
)
- disk: 用于存储生成文件的磁盘。
- base_destination: 所有生成文件的基础路径。
- template_default: 命令生成代码时使用的默认模板。
- templates: 包含操作、模板位置和最终动态目标列表的模板。
📑 模板
替换
%EntityNamespace%
: 在草图命名空间中的推荐用法。%PascalCase%
: 实体名称的 PascalCase 格式。%camelCase%
: 实体名称的 camelCase 格式。
🛠️ Artisan 命令
code:generate
选项
--force
: 强制覆盖现有文件(请谨慎使用)。
提示
- 实体: 要创建的实体名称。允许子路径(例如,
Path1/Path2/EntityName
)。 - 模板: 从终端中建议的选项中选择一个模板编号。