用过Laravel
的都知道,Laravel
通过php artisan make:controller
可以生成控制器,同样的夜可以用命令生成中间介和模型,那怎么自定义生成文件呢?
1.创建command类
<?php
namespace 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文件下创建自定义模板文件
<?php
namespace DummyNamespace;
class DummyClass
{
public function __construct()
{
}
}
创建了一个只有构造函数的类,具体模板可以自己定义
运行测试
php artisan make:service Web/TestService
这个时候Services
文件下的Web
目录下会生成TestService
文件,Web
目录不存在时会自动创建
网友评论