codedungeon / laravel-craftsman
Laravel Craftsman
Requires
- php: ^7.1.3
- codedungeon/php-messenger: ^1.0
- codedungeon/phpunit-result-printer: ^0.27
- illuminate/log: 5.8.*
- laravel-zero/framework: 5.8.*
- mustache/mustache: ^2.12
- nunomaduro/laravel-console-menu: ^2.1
- nunomaduro/phpinsights: ^1.9
- padraic/phar-updater: ^1.0.6
- phpunit/phpunit: ^8.1
Requires (Dev)
- mockery/mockery: ^1.0
- phpstan/phpstan: ^0.12.10
- spatie/phpunit-watcher: ^1.8
- dev-master
- 1.12.0
- 1.11.1
- 1.11.0
- 1.10.1
- 1.9.0
- 1.8.0
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.3
- 1.6.1
- 1.6.0
- 1.5.0
- 1.4.1
- 1.4.0
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.3
- 1.1.2
- 1.1.1
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-create-cast-generator
- dev-feature/migration-support-foreign-key
- dev-feature/create-wizard
- dev-dev/add-all-option-when-creating-model
- dev-dev/controller-route-model-binding
- dev-dev/create-resource-controller
- dev-002/migration-model-optional
- dev-001/template-option
- dev-feature/add-craft-views
This package is auto-updated.
Last update: 2024-09-09 08:06:34 UTC
README
描述
Laravel Craftsman(使用出色的 Laravel-Zero CLI 构建器编写)提供了一个使用项目无关的 CLI 的制作资源套件。
您可以使用单个命令快速创建 class
、command
、controller
、event
、factory
、form-request
、listener
、migration
、model
、resource
、rule
、seed
、test
和 view
资产。
此外,您可以使用单个命令创建所有资产,让您在几秒钟内快速制作新的资源!
目录
安装
使用 Composer
> composer global require codedungeon/laravel-craftsman
使用 curl/wget
> curl -o laravel-craftsman https://github.com/mikeerickson/laravel-craftsman/archive/master.zip or > wget https://github.com/mikeerickson/laravel-craftsman/archive/master.zip
用法
> laravel-craftsman <command> [options] [arguments] > laravel-craftsman interactive > laravel-craftsman interactive --silent > laravel-craftsman publish > laravel-craftsman publish --overwrite > laravel-craftsman craft:all Post --model App/Models/Post --tablename posts --rows 50 --extends layouts.app --section content > laravel-craftsman craft:api --model App/TestClass --overwrite > laravel-craftsman craft:class App/TestClass --constructor > laravel-craftsman craft:controller PostController --model App/Models/Post > laravel-craftsman craft:event ContactCreated > laravel-craftsman craft:event ContactCreated --no-broadcast > laravel-craftsman craft:factory PostFactory --model App/Models/Post > laravel-craftsman craft:migration create_posts_table --table posts > laravel-craftsman craft:model App/Models/Post --table posts > laravel-craftsman craft:model App/Models/Post --table posts --migration > laravel-craftsman craft:request CustomerRequest --rules "title?required|unique|posts,body?required" > laravel-craftsman craft:rule Uppercase > laravel-craftsman craft:seed PostTableSeeder --model App/Models/Post --rows 100 > laravel-craftsman craft:views authors --extends partials.master --section content
命令
以下命令在任何 Laravel 项目中都可使用。您可以使用类似于 Artisan 命令的单个制作程序,但 craft:all
命令是最强大的。
使用 craft:all
您可以轻松为给定资源(例如 Post、Customer 等)生成所有资产(控制器、工厂、迁移、模型和种子)
laravel-craftsman craft:all Contact \
--model App/Models/Contact \
--tablename contacts \
--rows 50 \
--fields "first_name:string@30:nullable,last_name:string@50:nullable,email:string@80:nullable:unique"
🚩 必要参数 / 选项
| craft:rule | | 创建验证规则 | | | 🚩 规则名称 | 规则名称(例如 Uppercase) | | | --template, -t | 自定义模板路径(覆盖配置文件) | | | --overwrite, -w | 覆盖现有类 | | craft:seed | | 使用提供的选项创建种子文件 | | | 🚩 种子名称 | 种子名称(例如 ContactTableSeeder) | | | --model, -m | 模型路径(例如 App/Models/Post) | | | --factory, -f | 创建工厂(如果尚不存在) | | | --rows, -r | 工厂调用中使用的行数(可选) | | | --template, -t | 自定义模板路径(覆盖配置文件) | | | --overwrite, -w | 覆盖现有类 | | craft:test | | 使用提供的选项创建种子文件 | | | 🚩 测试名称 | 测试名称(例如 CreateFileTest) | | | --setup, -s | 包含 setUp 方法 | | | --teardown, -d | 包含 tearDown 方法 | | | --unit, -u | 创建单元测试(默认为功能测试) | | | --pest, -p | 创建 Pest 测试 | | | --template, -t | 自定义模板路径(覆盖配置文件) | | | --overwrite, -w | 覆盖现有类 | | craft:views | 🚩 基础资源 | 种子名称 | | | --setup, -s | 包含设置块 | | | --teardown, -d | 包含 tearDown 块 | | | --unit, -u | 创建单元测试 | | | --template, -t | 自定义模板路径(覆盖配置文件) | | | --overwrite, -w | 覆盖现有类 | | craft:views | 🚩 基础资源 | 创建视图文件 | | | --extends, -x | 使用提供的布局包含扩展块 | | | --section, -s | 使用提供的名称包含部分块 | | | --no-create, -c | 排除创建视图 | | | --no-edit, -d | 排除编辑视图 | | | --no-index, -i | 排除索引视图 | | | --no-show, -w | 排除显示视图 |
定义类路径
当创建在分配目录中未自动创建的资源时,您可以按如下方式定义资产创建位置的路径
> laravel-craftsman craft:class App/Services/Sync ...
这将创建一个位于 App/Services
路径下的类,文件名为 Sync.php
。如果目录(包括嵌套目录)不存在,则会自动创建。
支持的命令
以下命令支持定义类路径
- craft:class
- craft:event
- craft:factory
- craft:listener
- craft:model
- craft:seed
- craft:test
- craft:views
📝 模板访问
Laravel Craftsman 将使用位于 laravel-craftsman
安装位置的合理默认模板。如果您希望对模板有更大的控制权,可以将默认模板发布(请参阅 laravel-craftsman publish
命令)到项目目录中(<project>/templates
)。
后续的 laravel-craftsman craft:xxx
命令将首先在项目模板目录中查找,如果没有找到模板,则将使用应用程序模板。
单次使用模板
除了标准模板外,您还可以定义一个仅在命令执行期间使用的单次使用模板。单次使用模板旨在引用项目特定的模板,并在执行所需命令时使用 <projet>
关键字。
> laravel-craftsman craft:class App/Services/SyncService --template "<project>/templates/service.mustache" ...
oh-my-zsh 冲突
如果您已安装 oh-my-zsh
,请确保将模板值用引号括起来,否则您可能会收到错误。
laravel-craftsman craft:class TestService --template <project>/templates/custom.mustache --overwrite zsh: no such file or directory: project
外键语法
当构建迁移时使用 --foreign
选项时,应使用以下语法
format: foreignKey:primaryId,primaryTable example: --foreign=post_id:id,posts -r=post_id:id,posts
或者,您也可以仅提供外键部分(使用 table_key 格式),它将用于提取主表和键。主表将是第一部分的复数形式,后面跟着主键 id。
--foreign=post_id
将被内部翻译为使用完整的 --foreign
格式
--foreign=post_id:id,posts
自动创建外键字段
当使用 --foreign
标志时,将在迁移文件中自动添加适当的字段。例如,如果提供了 --foreign post_id
标志,以下内容将被添加到新的迁移中
... Schema::create('comments', function (Blueprint $table) { $table->bigIncrements('id'); $table->unsignedBigInteger('post_id'); ... $table->foreign('post_id')->references('id')->on('posts'); }); ...
字段选项语法
当构建迁移时使用 --fields
选项时,应使用以下语法:注意:如果您已如上所述使用 --foreign
标志,则将自动添加外键字段。
format: fieldName:fieldType@fieldSize:option1:option2:option3 example: email:string@80:nullable:unique --fields "fname:string@25:nullable,lname:string@50:nullable,email:string@80:nullable:unique,dob:datetime,notes:text,deleted_at:timezone" Schema::create('contacts', function (Blueprint $table) { $table->bigIncrements('id'); $table->timestamps(); $table->string('fname', 25)->nullable(); $table->string('lname', 50)->nullable(); $table->string('email', 80)->nullable()->unique(); $table->datetime('dob'); $table->text('notes'); $table->timezone('deleted_at'); });
规则选项语法
当构建表单请求时使用 --rules
选项时,应使用以下语法
format: ruleName?rule1|rule2|rule3,ruleName2?rule1|rule2
> laravel-craftsman craft:request CustomerRequest --rules "title?required|unique|posts,body?required"
生成以下内容
public function rules() { return [ "title" => "required|unique|posts", "body" => "required", ]; }
提示
💡 布尔选项快捷方式
当执行任何 laravel-craftsman
命令时,如果您希望应用一个或多个开关(不需要对应值的选项),可以使用标准的 CLI 简写(此提示可用于任何基于 CLI 的工具,假设 CLI 实际支持简写)。
例如
假设您希望创建 views
,您可以使用以下命令跳过创建创建 (-c)、编辑 (-d) 和显示 (-w) 视图(只创建索引视图)。-cdw
组合是 --no-create --no-edit --no-show
的简写。
> laravel-craftsman craft:views --extends layouts.app --section content -cdw
等同于
> laravel-craftsman craft:views --extends layouts.app --section content --no-create --no-edit --no-show > laravel-craftsman craft:views --extends layouts.app --section content -c -d -w
💡 定义嵌套路径
任何命令都可以通过在默认路径中测试文件夹内通过斜杠分隔 name
参数来存储资产。例如,以下命令将定义模型资产在 App/Models/<name>
路径中的路径。
> laravel-craftsman App/Models/Customer ...
自定义模板
Laravel Craftsman 提供了创建自定义模板的支持,如果您想更改语法以匹配您的个人风格。默认模板使用标准的 Laravel 语法,但我们希望您有自己的风格(有关更大的模板控制,请参阅 laravel-craftsman publish
)。
📝 用户自定义模板
如果您想创建支持模板的衍生品,可以自定义位于 laravel-craftsman
目录下的 config.php
文件。默认情况下,它将是 ~/.composer/vendor/codedungeon/laravel-craftsman
,但可能根据您选择安装 laravel-craftsman 的方法而有所不同。
'templates' => [ 'class' => 'user_templates/class.mustache', 'api-controller' => 'user_templates/api-controller.mustache', 'binding-controller' => 'user_templates/binding-controller.mustache', 'empty-controller' => 'user_templates/empty-controller.mustache', 'command' => 'user_templates/command.mustache', 'controller' => 'user_templates/controller.mustache', 'events' => 'user_templates/event.mustache', 'factory' => 'user_templates/factory.mustache', 'listener' => 'user_templates/listener.mustache', 'migration' => 'user_templates/migration.mustache', 'model' => 'user_templates/model.mustache', 'request' => 'user_templates/request.mustache', 'rule' => 'user_templates/rule.mustache', 'seed' => 'user_templates/seed.mustache', 'test' => 'user_templates/tested.mustache', 'view-create' => 'user_templates/view-create.mustache', 'view-edit' => 'user_templates/view-edit.mustache', 'view-index' => 'user_templates/view-index.mustache', 'view-show' => 'user_templates/view-show.mustache', ],
📝 单次使用模板
除了创建模板和配置 config.php
文件外,您还可以选择性地提供一个作为单次使用(不存储)的模板。例如,如果您想创建一个标准类资产,可以使用以下单次使用模板:
占位符表示当前项目目录 ./ 占位符表示当前项目目录 占位符表示计算机根目录
> laravel-craftsman craft:class App/Services/Syncronize --template "<project>/templates/service.mustache" > laravel-craftsman craft:class App/Services/Syncronize --template "./templates/model.mustache" > laravel-craftsman craft:class App/Services/Syncronize --template "<root>/templates/model.mustache"
📝 模板变量
以下变量可以在任何支持的模板中使用(查看 templates
目录以了解如何创建自定义模板的基础知识)
许可
版权所有 © 2019-2020 Mike Erickson 在MIT许可下发布
致谢
laravel-craftsman 由 Mike Erickson 编写
推特:@codedungeon