ThinkPHP 使用命令行 (cli) think 调用

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

  1. 首先,在application\command目录(目录没有则创建)下新建hello.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(没有则创建)
<?php return [ "app\command\hello", ];
  1. cd到项目根目录,在命令行输入
php think hello
  1. OK,成功调用
hello world

__EOF__

本文作者织梦行云
本文链接https://www.cnblogs.com/cxfs/p/13206204.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   遗失的美好灬  阅读(486)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
点击右上角即可分享
微信分享提示