溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發(fā)布時間:2020-12-11 12:36:08 來源:億速云 閱讀:359 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)ThinkPHP如何使用命令行調(diào)用的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。

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

  1. 首先,在application\command目錄(目錄沒有則創(chuàng)建)下新建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')
             // 配置一個參數(shù) 使用$input->getArgument('username')獲取
             // ->addArgument('username')
             // 運行 "php think list" 時的簡短描述
             ->setDescription('定時任務微服務.')
             // 運行命令時使用 "--help" 選項時的完整命令描述
             ->setHelp("定時任務微服務 無參數(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 [
     "app\command\hello",];
  3. cd到項目根目錄,在命令行輸入

    php think hello
  4. OK,成功調(diào)用

    hello world


感謝各位的閱讀!關(guān)于ThinkPHP如何使用命令行調(diào)用就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI