step1 找到yii2项目根目录

step2 在advanced/console/controllers下新建文件HelloController.php

<?php
namespace console\controllers;
use yii\console\Controller;
class HelloController extends Controller
{
  /**
   * This command echoes what you have entered as the message.
   * @param string $message the message to be echoed.
   */
  public function actionIndex($message = 'hello world')
  {
    echo $message . "\n";
  }
}

step3 控制台执行

php ../yii 控制器名/action名

示例:

advanced/console# php ../yii hello/index 
或者
advanced# php yii hello/index "amyflash.com"

参考:https://www.bbsmax.com/A/mo5kyrEKzw/