ThinkPHP5使用cmd命令行 (cli) 执行 php think的方式

前言:

在某些场景里,我们需要在cmd命令行中调用代码,比如定时执行脚本采集数据,这时候 php  think就真正的派上了用场。

步骤:

一、首先,在application\command目录(目录没有则创建)下新建task.php,代码如下:

setName('task');
        // 2.配置一个参数 使用$input->getArgument('username')获取
        $this->addArgument('username');
        // 3.运行 "php think list" 时的简短描述
        $this->setDescription('执行定时任务');
        // 4.运行命令时使用 "--help" 选项时的完整命令描述
        $this->setHelp("执行定时任务 无参数");
    }

    /**
     *  * 重写execute
     *  * {@inheritdoc}
     *
     * @param Input $input
     * @param Output $output
     */
    protected function execute(Input $input, Output $output)
    {
        echo 'hello world';
    }
}

二、修改application/command.php(没有则创建),代码如下:

三、在网站的根目录下,执行cmd命令:

php think task

成功调用 ,输出信息如下:

hello world



 

你可能感兴趣的:(ThinkPHP)