TP6.0 自定義命令創(chuàng)建類文件

[TOC]

一、修改框架核心擴展包

1. 新增指令配置項


打開文件

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

在 類屬性 defaultCommands 中添加以下內(nèi)容

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

2. 創(chuàng)建邏輯層類文件模板


創(chuàng)建以下文件的一個副本

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

將副本重命名為 logic.stub, 文件內(nèi)容修改如下

<?php
declare (strict_types = 1);

namespace {%namespace%};

class {%className%}
{
    
}

3. 創(chuàng)建 Logic.php 文件


創(chuàng)建以下文件的一個副本

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

將副本重命名為 Logic.php, 修改內(nèi)容如下圖

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

4. 執(zhí)行命令, 創(chuàng)建邏輯層類文件


執(zhí)行命令

php think make:logic common@User

生成邏輯層類文件

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

二、不用修改框架源碼 【推薦】

1. 創(chuàng)建一個自定義命令類文件 (以邏輯層類文件為例)


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

php think make:command make/Logic

2. 復(fù)制創(chuàng)建模型類的命令定義文件內(nèi)容


復(fù)制以下文件內(nèi)容, 粘貼到 app\command\make\Logic.php 文件中

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

修改內(nèi)容如下圖

image

3. 拷貝命令行生成模型類的模板


拷貝命令行生成模型類的模板, 粘貼到 app\command\make\stubs\logic.stub

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

文件內(nèi)容如下

<?php
declare (strict_types = 1);

namespace {%namespace%};

class {%className%}
{
    /**
     * 邏輯層靜態(tài)方法
     */
    public static function demo()
    {

    }
}

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


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

5. 執(zhí)行自定義命令, 創(chuàng)建邏輯層類文件

php think make:logic api@User
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容