• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

ThinkPHP-使用命令行-cli-think-调用

php 搞代码 3年前 (2022-03-01) 14次浏览 已收录 0个评论

在某些场景里,我们需要在命令行中调用代码

  1. 首先,在applicationcommand目录(目录没有则创建)下新建hello.php:
<code class="php"><?php
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\Request;
class hello extends Command {
    /**
     * 重写configure
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this
            // 命令的名字("think" 后面的部分)
            ->setName('hello')
            // 配置一个参数 使用$input->getArgument('username')获取
            // ->addArgument('username')
            // 运行 "php think list" 时的简短描述
            ->setDescription('定时任务微服务.')
            // 运行命令时使用 "--help" 选项时的完整命令描述
            ->setHelp("定时任务微服务 无参数");
    }
    /**
     *  * 重写execute
     *  * {@inheritdoc}
     *  
     * @param Input $input
     * @param Output $output
     */
    protected function execute(Input $input, Output $output)
    {
        echo 'hello world';
    }
}
  1. 修改application/command.php(没有则创建)
<code class="php"><?php
return [
    "app\command\hello",
];
  1. cd到项目根目录,在命令行输入
<code class="sh"> php think hello
  1. OK,成功调用
<code class="sh"> hello world

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:ThinkPHP-使用命令行-cli-think-调用
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址