首頁  >  文章  >  php框架  >  ThinkPHP如何使用命令列 (cli) think調用

ThinkPHP如何使用命令列 (cli) think調用

coldplay.xixi
coldplay.xixi轉載
2020-06-30 17:47:244628瀏覽

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(&#39;hello&#39;)
             // 配置一个参数 使用$input->getArgument(&#39;username&#39;)获取
             // ->addArgument(&#39;username&#39;)
             // 运行 "php think list" 时的简短描述
             ->setDescription(&#39;定时任务微服务.&#39;)
             // 运行命令时使用 "--help" 选项时的完整命令描述
             ->setHelp("定时任务微服务 无参数");
     }
     /**
      *  * 重写execute
      *  * {@inheritdoc}
      *  
      * @param Input $input
      * @param Output $output
      */
     protected function execute(Input $input, Output $output)
     {
         echo &#39;hello world&#39;;
     }}
  2. 修改application/command.php(沒有則建立)
    <?php
    return [
     "app\command\hello",];
  3. cd到專案根目錄,在指令列輸入
    php think hello
  4. OK,成功呼叫
    hello world

相關學習推薦:PHP程式設計從入門到精通

以上是ThinkPHP如何使用命令列 (cli) think調用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:learnku.com。如有侵權,請聯絡admin@php.cn刪除