author:咔咔
wechat:fangkangfk
在项目开发时,使用命令创建文件可以保障你的出错率
首先使用php artisan make:command TestMake创建出自定义命令文件
然后复制一份框架创建文件的源码
将里边所有的event改为server即可,切记区分大小写,下面是一份源码
<?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
下来测试命令,创建php artisan make:server LoginServerCommand,创建成功