[记录]在cmd内运行ThinkPHP5目录内的php文件

众所周知,cmd运行php单文件的命令是

E:\test>php test.php

但是自己写数据库连接或者一些其他东西就很繁琐
直接用tp就很方便
上面的命令是不适用于执行ThinkPHP内php文件的

那么在cmd怎么执行ThinkPHP内的php文件呢?

准备工作:

首先在 app > command.php 内添加如下代码

return [
	'app\index\command\Test',
];

二.在app > index 目录下创建名为 command 的文件夹, 再创建一个名为 Test.php 的文件

Test.php 内写入如下代码

<?php
namespace app\index\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;

class Test extends Command
{
    protected function configure()
    {
        $this->setName('Test')->setDescription('Here is the remark ');
    }
 
    protected function execute(Input $input, Output $output)
    {   
        $output->writeln("TestCommand");
        // 在这里写你要执行的业务代码
    }

}

以上我们的准备工作就做完了~

打开小黑框:

然后我们可以打开 cmd 运行一下

注意:cmd目录切换到tp5的根目录就可以
输入:
E:\test>php think Test
输出:
TestCommand

-End-

风影OvO

风影OvO, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA 4.0协议进行授权 | 转载请注明原文链接

留下你的评论

*评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

相关推荐