yii2 console unknown命令

时间:2018-03-02 11:49:21

标签: yii2 console yii2-advanced-app yii2-basic-app

尝试在控制器_googleanalytics中运行函数ProcessingController,但收到错误:

  

未知命令

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,
];

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您需要让action通过控制台/终端访问它,就像我们通过浏览器访问操作一样。

例如,如果我在Test Controller目录

中创建如下console/controllers
<?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

你可以用引号括起来但是没有必要在单独的参数之间添加一个空格标识。

希望有所帮助