您好,欢迎访问一九零五行业门户网

Laravel怎么自定义command命令

laravel自定义command命令的方法:首先创建command类;然后在“stubs”文件下创建自定义模板文件;最后通过“php artisan make:service web/testservice”运行测试即可。
laravel自定义command命令
用过laravel的都知道,laravel通过php artisan make:controller可以生成控制器,同样的也可以用命令生成中间介和模型,那怎么自定义生成文件呢?
1.创建command类
<?phpnamespace app\console\commands;use illuminate\console\generatorcommand;class servicemakecommand extends generatorcommand{ /** * the console command name. * * @var string */ protected $name = 'make:service'; /** * the console command description. * * @var string */ protected $description = 'create a new service class'; /** * the type of class being generated. * * @var string */ protected $type = 'services'; /** * get the stub file for the generator. * * @return string */ protected function getstub() { return __dir__.'/stubs/service.stub'; } /** * get the default namespace for the class. * * @param string $rootnamespace * @return string */ protected function getdefaultnamespace($rootnamespace) { return $rootnamespace."\services"; }}
2.在commands/stubs文件下创建自定义模板文件
<?phpnamespace dummynamespace;class dummyclass { public function __construct() { }}
创建了一个只有构造函数的类,具体模板可以自己定义
运行测试
php artisan make:service web/testservice
这个时候services文件下的web目录下会生成testservice文件,web目录不存在时会自动创建
其它类似信息

推荐信息