Search code examples
yii2consoleyii2-advanced-appyii2-basic-app

yii2 console unknown command


Trying to run function _googleanalytics in controller ProcessingController, but getting an error:

unknown command

command:

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

controller path:

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

what I am doing wrong?


Solution

  • You need to make an action to access it via console/terminal the same way as we access the actions via our browser.

    For example if i create a Test Controller like below inside console/controllers directory

    <?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";
        }
    }
    

    and then type ./yii and hit Enter it will show all default commands available along with the following at the end.

    This is Yii version 2.0.14.1.
    
    The following commands are available:
    ....
    ...
    - test                             
        test/index (default)
        test/mango
    

    which means it registers all the actions inside the controller as commands and if you write in the terminal the following command,

    ./yii test/index omer aslam

    it will show you the output

    Index
    omer aslam
    

    where omer and aslam are the 2 params passed to the function.

    So you just need to prepend keyword action to your function name i would suggest using action names according to the convention, change the function from

    public function _googleanalytics($start, $finish) {
    

    to

    public function actionGoogleanalytics($start, $finish) {
    

    and then access it via

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

    you can wrap with quotes but it isnt necessary to add one a space identitifies between separate params.

    Hope it helps