author:咔咔
在項目開發時,使用指令建立檔案可以保障你的出錯率
首先使用php artisan make:command TestMake建立出自定義指令檔案
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsISPrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdsATOfd3bkFGazxCMx8VesATMfhHLlN3XnxCMwEzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsYTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5SOxIDMlVDZiRmM2U2YwImYhZzMxAjMkFTYkRmMjZjNm9CX3IzLclDMxIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjL4M3Lc9CX6MHc0RHaiojIsJye.png)
<?php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
class ServerMakeCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:server';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new server class';
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Server';
/**
* Determine if the class already exists.
*
* @param string $rawName
* @return bool
*/
protected function alreadyExists($rawName)
{
return class_exists($rawName);
}
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.'/stubs/server.stub';
}
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Server';
}
}
然後需要建立模闆檔案server.stub