zl程序教程

您现在的位置是:首页 >  其他

当前栏目

TP6.0 自定义命令创建类文件

文件命令 创建 自定义 TP6.0
2023-06-13 09:16:43 时间

一、修改框架核心扩展包

1. 新增指令配置项


打开文件

vendor\topthink\framework\src\think\Console.php

在 类属性 defaultCommands 中添加以下内容

'make:logic' => \think\console\command\make\Logic::class,

2. 创建逻辑层类文件模板


创建以下文件的一个副本

vendor\topthink\framework\src\think\console\command\make\stubs\model.stub

将副本重命名为 logic.stub, 文件内容修改如下

<?php
declare (strict_types = 1);
namespace {%namespace%};
class {%className%}
{
}

3. 创建 Logic.php 文件


创建以下文件的一个副本

vendor\topthink\framework\src\think\console\command\make\Model.php

将副本重命名为 Logic.php, 修改内容如下图

vendor\topthink\framework\src\think\console\command\make\Logic.php

4. 执行命令, 创建逻辑层类文件


执行命令

php think make:logic common@User

生成逻辑层类文件

Logic:app\common\logic\User created successfully.

二、不用修改框架源码 【推荐】

1. 创建一个自定义命令类文件 (以逻辑层类文件为例)


生成 app\command\make\Logic.php 文件

php think make:command make/Logic

2. 复制创建模型类的命令定义文件内容


复制以下文件内容, 粘贴到 app\command\make\Logic.php 文件中

站长源码网

vendor\topthink\framework\src\think\console\command\make\Model.php

修改内容如下图

3. 拷贝命令行生成模型类的模板


拷贝命令行生成模型类的模板, 粘贴到 app\command\make\stubs\logic.stub

vendor\topthink\framework\src\think\console\command\make\stubs\model.stub

文件内容如下

<?php
declare (strict_types = 1);
namespace {%namespace%};
class {%className%}
{
    /**
     * 逻辑层静态方法
     */
    public static function demo()
    {
    }
}

4. 将自定义命令添加到指令配置文件 config\console.php


return [
    // 指令定义
    'commands' => [
        'make:logic' => app\command\make\Logic::class,
    ],
];

5. 执行自定义命令, 创建逻辑层类文件

php think make:logic api@User