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
相关文章
- linux命令chmod 777_chmod无法访问 没有那个文件或目录
- linux文件的创建与扫描,Linux系统quotacheck命令:扫描文件系统并建立Quota记录文件…
- RAID5崩溃后的文件恢复
- html5 文件api使用示例详解编程语言
- 命令使用Linux colrm命令删除文件中的列(linuxcolrm)
- Linux系统下创建TXT文件的命令(linux创建txt命令)
- sed命令_Linux sed命令:替换、删除、更新文件中的内容
- Linux locate命令:按照文件名搜索文件
- 压缩 WAR 文件:Linux 上的挑战(linux压缩war)
- 掌握 Linux 的 wc 命令,轻松统计文件信息(linuxwc命令)
- 如何用MySQL读取txt文件?(mysql读取txt)
- 使用 Linux dd 命令创建文件:从入门到精通(linuxdd创建文件)
- Linux教程:用命令创建文件(linux用命令创建文件)
- 25个Linux查看文件的命令一览(linux文件查看命令)
- 「Linux教程:学习如何通过创建文件命令在Linux系统中创建新文件」(创建文件命令 linux)
- Linux中打开文件的命令之旅(打开文件命令 linux)