1、创建 command 命令
创建 Controller模板、Service 模板、Model 模板、Api Resource模板
1. php artisan make:command MakeController # Controller模板
2. php artisan make:command MakeService #Service 模板
3. php artisan make:command MakeModel #Model 模板
4. php artisan make:command MakeResource #Api Resource模板
5. php artisan make:command MakeCollection #Api Resource模板
6. php artisan make:command MakeModule #其他模板集合
生成效果如下:
2、各个命令文件详情
1、MakeController
<?php
namespace App\Console\Commands\Generator;
use Illuminate\Console\Command;
use Illuminate\Console\GeneratorCommand;
class MakeController extends GeneratorCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'generator:controller {name}';
/**
* The console command description.
*
* @var string
*/
protected $description = '生成controller对象类';
protected $type = "Controller";
protected function getStub()
{
return base_path("stubs\controller.stub");
}
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace."\Http\Controllers\V1";
}
}
2、MakeService
<?php
namespace App\Console\Commands\Generator;
use Illuminate\Console\Command;
use Illuminate\Console\GeneratorCommand;
class MakeService extends GeneratorCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'generator:service {name}';
/**
* The console command description.
*
* @var string
*/
protected $description = '生成service对象类';
protected $type = "Service";
protected function getStub()
{
return base_path("stubs\\service.stub");
}
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace."\Repositories\Services";
}
}
3、MakeModel
<?php
namespace App\Console\Commands\Generator;
use Illuminate\Console\Command;
use Illuminate\Console\GeneratorCommand;
class MakeModel extends GeneratorCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'generator:model {name}';
/**
* The console command description.
*
* @var string
*/
protected $description = '生成model对象类';
protected $type = "Model";
protected function getStub()
{
return base_path("stubs\\model.stub");
}
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace."\Repositories\Models";
}
}
4、MakeResource
<?php
namespace App\Console\Commands\Generator;
use Illuminate\Console\Command;
use Illuminate\Console\GeneratorCommand;
class MakeResource extends GeneratorCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'generator:resource {name} {raw-name?}';
/**
* The console command description.
*
* @var string
*/
protected $description = '生成resource-collection对象类';
protected $type = "Resource";
protected function getStub()
{
return base_path("stubs\\resource\\resource.stub");
}
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace."\Http\Resources\\".$this->argument("raw-name")?:$this->argument("name");
}
}
5、MakeCollection
<?php
namespace App\Console\Commands\Generator;
use Illuminate\Console\Command;
use Illuminate\Console\GeneratorCommand;
class MakeCollection extends GeneratorCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'generator:collection {name} {raw-name?}';
/**
* The console command description.
*
* @var string
*/
protected $description = '生成resource-collection对象类';
protected $type = "Collection";
protected function getStub()
{
return base_path("stubs\\resource\\collection.stub");
}
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace."\Http\Resources\\".$this->argument("raw-name")?:$this->argument("name");
}
}
6、MakeModule
<?php
namespace App\Console\Commands;
use App\Console\Commands\Generator\MakeCollection;
use App\Console\Commands\Generator\MakeController;
use App\Console\Commands\Generator\MakeModel;
use App\Console\Commands\Generator\MakeResource;
use App\Console\Commands\Generator\MakeService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
class MakeModule extends Command
{
/**
* 控制台命令的名称和签名
*
* @var string
*/
protected $signature = 'generator:module {name}';
/**
* 命令描述
*
* @var string
*/
protected $description = '生成模板文件';
public function handle()
{
$this->generatorController();
$this->generatorModel();
$this->generatorService();
$this->generatorResource();
}
/**
* 生成 Controller 类
*/
private function generatorController()
{
$name = $this->argument("name");
Artisan::call(MakeController::class,['name'=>$name.'Controller']);
$this->line("Generator Controller Success");
}
/**
* 生成 Service 类
*/
private function generatorService()
{
$name = $this->argument("name");
Artisan::call(MakeService::class,['name'=>$name.'Service']);
$this->line("Generator Service Success");
}
/**
* 生成 Model 类
*/
private function generatorModel()
{
$name = $this->argument("name");
Artisan::call(MakeModel::class,['name'=>$name]);
$this->line("Generator Model Success");
}
/**
* 生成资源类
*/
private function generatorResource()
{
$name = $this->argument("name");
Artisan::call(MakeResource::class,['name'=>$name.'Resource','raw-name'=>$name]);
Artisan::call(MakeCollection::class,['name'=>$name.'Collection','raw-name'=>$name]);
$this->line("Generator Resource Success");
}
}
3、创建 stub 模板文件
模板目录文件结构:
1、controller.stub
<?php
namespace DummyNamespace;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class DummyClass extends Controller
{
public function index()
{
//
}
public function store(Request $request)
{
//
}
public function show($id)
{
//
}
public function update(Request $request, $id)
{
//
}
public function destroy($id)
{
//
}
}
2、service.stub
<?php
namespace DummyNamespace;
class DummyClass
{
//
}
3、model.stub
<?php
namespace DummyNamespace;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class DummyClass extends Model
{
use HasFactory;
public $guarded=["*"];
}
4、resource.stub
<?php
namespace DummyNamespace;
use Illuminate\Http\Resources\Json\JsonResource;
class DummyClass extends JsonResource
{
public function toArray($request)
{
return parent::toArray($request);
}
}
5、collection.stub
<?php
namespace DummyNamespace;
use Illuminate\Http\Resources\Json\ResourceCollection;
class DummyClass extends ResourceCollection
{
public function toArray($request)
{
return parent::toArray($request);
}
}