yii2 控制台未知命令

yii2 console unknown command

正在控制器 ProcessingController 中尝试 运行 函数 _googleanalytics,但出现错误:

unknown command

command:

./yii processing/_googleanalytics '2017-02-27' '2017-02-27'

controller路径:

/console/controllers/

action

public function _googleanalytics($start, $finish) {...

controller

namespace console\controllers;
class ProcessingController extends Controller
{...

/console/config/main.php

return [
    'id' => 'app-console',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'console\controllers',
    'aliases' => [
        '@bower' => '@vendor/bower-asset',
        '@npm'   => '@vendor/npm-asset',
    ],
    'controllerMap' => [
        'fixture' => [
            'class' => 'yii\console\controllers\FixtureController',
            'namespace' => 'common\fixtures',
          ],
    ],
    'components' => [
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning','info'],
                    'exportInterval' => 1,
                ],
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['info'],
                    'exportInterval' => 1,
                    'logVars' => [],
                    'categories' => ['calls'],
                    'logFile' => '@app/runtime/logs/calls.log',
                    'maxFileSize' => 1024 * 2,
                    'maxLogFiles' => 20,
                ],
            ],
        ],
    ],
    'modules'=>[
        'user-management' => [
            'class' => 'webvimark\modules\UserManagement\UserManagementModule',
            'controllerNamespace'=>'vendor\webvimark\modules\UserManagement\controllers', // To prevent yii help from crashing
        ],
        'googleanalytics' => [
           'class' => 'console\modules\googleanalytics\Module',
        ]
    ],
    'params' => $params,
];

我做错了什么?

您需要创建一个 action 才能通过 console/terminal 访问它,就像我们通过浏览器访问操作一样。

例如,如果我在 console/controllers 目录

中创建如下所示的 Test Controller
<?php
namespace console\controllers;

class TestController extends \yii\console\Controller{
    public function actionIndex($param1,$param2){
        echo "\nIndex";
        echo "\n$param1 $param2\n";
    }

    public function actionMango(){
        echo "\nMango";
    }
}

然后键入 ./yii 并按 Enter 它将显示所有可用的默认命令以及最后的以下内容。

This is Yii version 2.0.14.1.

The following commands are available:
....
...
- test                             
    test/index (default)
    test/mango

这意味着它将控制器内的所有操作注册为命令,如果您在终端中写入以下命令,

./yii test/index omer aslam

它将显示输出

Index
omer aslam

其中 omeraslam 是传递给函数的 2 个参数。

所以你只需要在你的函数名称前加上关键字 action 我建议根据约定使用动作名称,将函数从

public function _googleanalytics($start, $finish) {

public function actionGoogleanalytics($start, $finish) {

然后通过

访问它

./yii process/googleanalytics 2017-02-27 2017-02-27

您可以用引号引起来,但没有必要在单独的参数之间添加一个 space 标识。

希望对您有所帮助