1、新建文件,文件名任意:
app/Helpers/function.php
2、在composer.json 中 autoload 增加:
“autoload”:{
    ...
    "files":[
        "app/Helpers/function.php"
    ]  
}
3、打开cmd 切换到项目目录 执行命令:
composer dump-auto1、新建文件,文件名任意:
app/Helpers/function.php
2、在composer.json 中 autoload 增加:
“autoload”:{
    ...
    "files":[
        "app/Helpers/function.php"
    ]  
}
3、打开cmd 切换到项目目录 执行命令:
composer dump-auto主要是方便快速写入代码,比如后端增删改查CRUD
1.创建命令文件
#php artisan make:command 命令文件名
php artisan make:command Validator
 创建命令文件
创建命令文件2.注册命令在/app/Console/Kernel.php中添加一下内容
protected $commands = [
    \App\Console\Commands\Validator::class
];
 注册命令
注册命令3.编写命令步骤如下:
先在/app/Console/Commands/validator/下创建validator.stub和default_method.stub文件(目录名称建议命令一致,在自定义多个的情况下方便区分)
 目录文件
目录文件validator.stub : 逻辑层的主要内容,包含命名空间和类
<?php
namespace {{namespace}};
# 看个人情况确定是否包含继承文件和导入文件
use Illuminate\Support\Facades\Validator;
class {{class_name}} extends Validator
{
    {{default_method}}
}
default_method.stub : 默认方法模板,根据--resource参数来确认是否写入logic.stub
public function index()
{
}
public function test()
{
    
}
相关Validator.php命令名称,参数和描述
/**
 * The name and signature of the console command.
 *
 * @var string
 */
protected $signature = 'make:validator {name} {--resource}';
/**
 * The console command description.
 *
 * @var string
 */
protected $description = 'create validator';
修改Validator.php命令文件中handle方法:列举如下
/**
 * Execute the console command.
 *
 * @return int
 */
public function handle()
{
    // 获取参数
    $args = $this->arguments();
    // 获取可选参数
    $option = $this->option('resource');
    // 处理组合参数
    $args_name = $args['name'];
    if (strstr($args['name'], '/')) {
        $ex = explode('/', $args['name']);
        $args_name = $ex[count($ex)-1];
        $namespace_ext = '/' . substr($args['name'], 0, strrpos($args['name'], '/'));
    }
    $namespace_ext = $namespace_ext ?? '';
    // 类名
    // $class_name = $args_name . 'Validator';
    $class_name = $args_name;
    //文件名
    $file_name = $class_name . '.php';
    // 文件地址
    $logic_file = app_path() . '/Validator' . $namespace_ext . '/' . $file_name;
    // 命名空间
    $namespace = 'App\Validator' . str_replace('/', '\\', $namespace_ext);
    // 目录
    $logic_path = dirname($logic_file);
    // 获取模板,替换变量
    $template = file_get_contents(dirname(__FILE__) . '/stubs/validator.stub');
    $default_method = $option ? file_get_contents(dirname(__FILE__) . '/stubs/default_method.stub') : '';
    $source = str_replace('{{namespace}}', $namespace, $template);
    $source = str_replace('{{class_name}}', $class_name, $source);
    $source = str_replace('{{default_method}}', $default_method, $source);
    // 是否已存在相同文件
    if (file_exists($logic_file)) {
        $this->error('文件已存在');
        exit;
    }
    // 创建
    if (file_exists($logic_path) === false) {
        if (mkdir($logic_path, 0777, true) === false) {
            $this->error('目录' . $logic_path . '没有写入权限');
            exit;
        }
    }
    // 写入
    if (!file_put_contents($logic_file, $source)) {
        $this->error('创建失败!');
        exit;
    }
    $this->info('创建成功!');
}
4.使用自定义命令:
#如果不需要默认方法,去掉--reource参数即可
#php artisan make:validator User --resource
php artisan make:validator User
 效果如下
效果如下