ThinkPHP如何使用命令行 (cli) think調(diào)用

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

ThinkPHP如何使用命令行 (cli) think調(diào)用

在某些場(chǎng)景里,我們需要在命令行中調(diào)用代碼

  1. 首先,在applicationcommand目錄(目錄沒有則創(chuàng)建)下新建hello.php
    <?php namespace appcommand; use thinkconsoleCommand; use thinkconsoleInput; use thinkconsoleinputArgument; use thinkconsoleinputOption; use thinkconsoleOutput; use thinkRequest; class hello extends Command {  /**   * 重寫configure   * {@inheritdoc}   */  protected function configure()  {      $this          // 命令的名字("think" 后面的部分)          ->setName('hello') ?????????//?配置一個(gè)參數(shù)?使用$input-&gt;getArgument('username')獲取 ?????????//?-&gt;addArgument('username') ?????????//?運(yùn)行?"php?think?list"?時(shí)的簡(jiǎn)短描述 ?????????-&gt;setDescription('定時(shí)任務(wù)微服務(wù).') ?????????//?運(yùn)行命令時(shí)使用?"--help"?選項(xiàng)時(shí)的完整命令描述 ?????????-&gt;setHelp("定時(shí)任務(wù)微服務(wù)?無參數(shù)"); ?} ?/** ??*??*?重寫execute ??*??*?{@inheritdoc} ??*?? ??*?@param?Input?$input ??*?@param?Output?$output ??*/ ?protected?function?execute(Input?$input,?Output?$output) ?{ ?????echo?'hello?world'; ?}}
  2. 修改application/command.php(沒有則創(chuàng)建)
    <?php return [  "appcommandhello",];
  3. cd到項(xiàng)目根目錄,在命令行輸入
    php?think?hello
  4. OK,成功調(diào)用
    hello?world

相關(guān)學(xué)習(xí)推薦:PHP編程從入門到精通

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊7 分享