arkadiuszbachorski / laravel-xmake
扩展Laravel Artisan Make命令
Requires
- php: ^7.2
- laravel/framework: >=5.8
README
为快速创建资源和脚手架而提供的额外的Laravel Artisan xmake命令。旨在加快开发过程,避免在各个地方重复输入相同的内容。
目录
入门
先决条件
此包是为Laravel 5.8及以上版本开发的。我还没有测试早期版本。
安装
仅使用composer开发时需要此包。
composer require arkadiuszbachorski/laravel-xmake --dev
发布配置
php artisan vendor:publish --tag=xmake-config
如果需要,可以更改默认的资源路径。
发布资源
php artisan vendor:publish --tag=xmake-resources
功能
- 更快地搭建应用程序
- 只需一条命令即可创建许多相关文件:模型、控制器、请求、资源、迁移、播种器和工厂
- 在同一个地方提供字段,其余的将由系统准备或自动填充
- 轻松自定义模板以满足您的需求
用法
你更喜欢看而不是读吗?
示例
php artisan xmake -i --fields=title,foo,bar --modelName=Foobar --model --request --controller --api
结果
FoobarController.php
<?php namespace App\Http\Controllers; use App\Foobar; use App\Http\Requests\FoobarRequest; class FoobarController extends Controller { /** * Display a listing of the resource. * * @return Response */ public function index() { $foobars = Foobar::all(); return response()->json([]); } /** * Show the form for creating a new resource. * * @return Response */ public function create() { return response()->json([]); } /** * Store a newly created resource in storage. * * @param FoobarRequest $request * @return Response */ public function store(FoobarRequest $request) { Foobar::create($request->validated()); return response()->json([]); } /** * Display the specified resource. * * @param Foobar $foobar * @return \Illuminate\Http\Response */ public function show(Foobar $foobar) { return response()->json([]); } /** * Show the form for editing the specified resource. * * @param Foobar $foobar * @return \Illuminate\Http\Response */ public function edit(Foobar $foobar) { return response()->json([]); } /** * Update the specified resource in storage. * * @param FoobarRequest $request * @param Foobar $foobar * @return \Illuminate\Http\Response */ public function update(FoobarRequest $request, Foobar $foobar) { $foobar->update($request->validated()); return response()->json([]); } /** * Remove the specified resource from storage. * * @param Foobar $foobar * @return \Illuminate\Http\Response * @throws \Exception */ public function destroy(Foobar $foobar) { $foobar->delete(); return response()->json([]); } }
Foobar.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Foobar extends Model { protected $guarded = []; }
FoobarRequest.php
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class FoobarRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return false; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'title' => '', 'foo' => '', 'bar' => '', ]; } }
挺不错的,对吧?有关更多详细信息,请参阅文档。
待办事项列表
- 基于验证和迁移字段类型猜测工厂
- 基于验证猜测迁移字段类型
文档
配置
config/xmake.php
[ 'paths' => [ // Path where stubs are expected to exist. This path affects vendors publishing too. 'stubs' => '/resources/xmake/stubs', // Path where fields.php is expected to exist. This path affects vendors publishing too. 'fields' => '/resources/xmake', ], 'database' => [ // Flag that indicates whether ->nullable() should be automatically added if provided in validation 'addNullableIfAppearsInValidation' => true, ], 'controller' => [ // You can change PHPDoc methods captions there 'docs' => [ 'index' => 'Display a listing of the resource.', 'create' => 'Show the form for creating a new resource.', 'store' => 'Store a newly created resource in storage.', 'show' => 'Display the specified resource.', 'edit' => 'Show the form for editing the specified resource.', 'update' => 'Update the specified resource in storage.', 'destroy' => 'Remove the specified resource from storage.', ], // You can change CRUD methods names there 'methods' => [ 'index' => 'index', 'create' => 'create', 'store' => 'store', 'show' => 'show', 'edit' => 'edit', 'update' => 'update', 'destroy' => 'destroy', ], ], 'seeder' => [ // Default amount used in seeders if not provided by --amount option 'defaultAmount' => 50, ], 'resource' => [ // Flag that indicates whether resource fields should be parsed to camelCase 'camelizeFields' => true, ], 'validation' => [ // It enables parsing pipe syntax (i.e. "string|nullable") to array 'parseArray' => true, // It enables guessing validation based on database field. I.e. string('foobar') parses to 'string' validation. 'guessBasedOnDatabase' => true, ], // You can change what will be created if you select "create everything"/"all" option 'createEverything' => [ 'model' => true, 'migration' => true, 'factory' => true, 'seeder' => true, 'request' => true, 'resource' => true, 'controller' => true, ], ];
模板
默认情况下,模板位于/resources/xmake/stubs路径中。您在那里可以看到一些文本文件。所有这些文件都包含"Dummy..."内容,这意味着它们将被生成的内容替换。所有不以"Dummy..."开头的内容都可以由您自定义。
字段
字段文件默认位于/resources/xmake/fields.php路径中。它是关于字段数据的来源。
[ // key used in --fields option 'title20ch' => [ /* Field name used in Eloquent, database migration. I.e $model->title */ 'name' => 'title', /* Used in factory after $faker-> I.e. 'title' => $faker->sentence(2) */ 'factory' => 'sentence(2)', 'validation' => 'string|nullable', /* Migration, NAME will by replaced with actual name automatically I.e. string('title', 20)->default('test'), */ 'database' => 'string(NAME, 20)->default("test")', ], ];
您不需要填写每个字段的全部数据。唯一必需的是"名称"。如果您将未知字段输入到--fields选项中,它将被视为键是名称,而其他参数为空。
这意味着如果您只想列出创建的文件中的每个字段,您可以完全忽略此文件。
但是,如配置中所示,Xmake有几个自动填充和数据处理的机制。如果您提供数据库字段,则有可能在验证和工厂中填充一些重复的内容。
命令
每个命令都有--help选项,因此您可以比在这里检查更快地查看可用的选项。
以下示例中,我假设默认配置和文件fields.php包含
[ 'foo' => [ 'name' => 'foo', 'factory' => 'sentence(2)', 'validation' => 'string|nullable', 'database' => 'string(NAME, 20)->default("test")', ], ];
xmake
这是一个最常用的命令,因为它提供了强大的脚手架能力。它可以调用下面列出的每个单个命令。
默认情况下,Xmake运行交互式shell体验。如果您想更快地创建资源,请使用--instant标志。结果几乎相同,但您不能独立命名文件。您可能根本不会这样做,因为自动命名工作得很好。
可用选项
--instant -i
如果您不想使用交互式shell,请使用此标志。
--modelName
此参数是必需的,几乎每个命令都使用它。
--fields
获取字段键数组,使用逗号作为分隔符
--all
根据您的配置创建一切。默认情况下,这是字面上的“一切”。
--api
创建API版本的控制器并启用资源创建
--model
使用给定的modelName创建模型
--migration
创建具有准备或填充字段的迁移
--factory
使用给定的字段创建工厂,已准备或填写
--seeder
创建一个调用工厂的生成器
--request
创建带有给定字段准备或填写的请求
--resource
使用给定字段准备或填写创建资源
--controller
创建控制器,根据您的选择,基于请求、资源和API的各种选项
xmake:model
它仅创建模型。
示例
php artisan xmake:model Foobar
结果
Foobar.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Foobar extends Model { protected $guarded = []; }
xmake:controller
它创建具有
- 模型的基本 CRUD 操作
- Blade 视图或 REST API
- 验证在内部或注入
可用选项
--model -m
它是必需的。您必须提供模型名称。
--api
它是一个标志,将响应从 Blade 视图更改为 REST API。
--fields
您可以在那里提供验证字段。
--resource -x
它将指定的资源注入索引、存储、显示、编辑和更新返回。如果没有提供 --api 标志,则不执行任何操作。
--request -r
它将指定的请求注入创建和更新方法。如果文件不存在,则可以使用您提供的字段创建它。
示例
php artisan xmake:controller FoobarController --api --fields=foo,bar --model=Foobar
结果
<?php namespace App\Http\Controllers; use App\Foobar; use Illuminate\Http\Request; class FoobarController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $foobars = Foobar::all(); return response()->json([]); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { return response()->json([]); } /** * Store a newly created resource in storage. * * @param Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $data = $request->validate([ 'foo' => 'string|nullable', 'bar' => '', ]); Foobar::create($data); return response()->json([]); } /** * Display the specified resource. * * @param Foobar $foobar * @return \Illuminate\Http\Response */ public function show(Foobar $foobar) { return response()->json([]); } /** * Show the form for editing the specified resource. * * @param Foobar $foobar * @return \Illuminate\Http\Response */ public function edit(Foobar $foobar) { return response()->json([]); } /** * Update the specified resource in storage. * * @param Request $request * @param Foobar $foobar * @return \Illuminate\Http\Response */ public function update(Request $request, Foobar $foobar) { $data = $request->validate([ 'foo' => 'string|nullable', 'bar' => '', ]); $foobar->update($data); return response()->json([]); } /** * Remove the specified resource from storage. * * @param Foobar $foobar * @return \Illuminate\Http\Response * @throws \Exception */ public function destroy(Foobar $foobar) { $foobar->delete(); return response()->json([]); } }
xmake:resource
它使用给定的字段创建资源。默认情况下,它将字段名称转换为驼峰式命名,但可以在配置中更改。
可用选项
--fields
示例
php artisan xmake:resource FoobarResource --fields=foo,bar,not_camel_case_field
结果
<?php namespace App\Http\Resources; use Illuminate\Http\Resources\Json\JsonResource; class FoobarResource extends JsonResource { /** * Transform the resource into an array. * * @param \Illuminate\Http\Request $request * @return array */ public function toArray($request) { return [ 'foo' => $this->foo, 'bar' => $this->bar, 'notCamelCaseField' => $this->not_camel_case_field, ]; } }
xmake:migration
它创建具有给定字段准备或填写的迁移。
可用选项
--create
表名
--fields
示例
php artisan xmake:migration create_foobar_table --create=foobar --fields=foo,bar
结果
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateFoobarTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('foobar', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('foo', 20)->default("test")->nullable(); $table->('bar'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('foobar'); } }
xmake:request
它创建带有给定验证规则准备或填写的请求。
示例
php artisan xmake:request FoobarRequest --fields=foo,bar
结果
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class FoobarRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'foo' => 'string|nullable', 'bar' => '', ]; } }
xmake:factory
它创建带有给定工厂规则准备或填写的工厂。
示例
php artisan xmake:factory FoobarFactory --fields=foo,bar
结果
<?php /* @var $factory \Illuminate\Database\Eloquent\Factory */ use App\Foobar; use Faker\Generator as Faker; $factory->define(Foobar::class, function (Faker $faker) { return [ 'foo' => $faker->sentence(2), 'bar' => $faker->, ]; });
xmake:seeder
它创建具有给定模型的生成器。
可用选项
--model
模型名称。
--amount
在生成器中创建的模型数量。默认值可以在配置中更改。
示例
php artisan xmake:seeder FoobarSeeder --model=foo,bar --amount=33
结果
<?php use Illuminate\Database\Seeder; use App\Foobar; class FoobarSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { factory(Foobar::class, 33); } }
总结
以上所有文件都可以通过一个命令创建
php artisan xmake -i --modelName=Foobar --all --api --fields=foo,bar