Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 console script not working

Tags:

php

console

yii2

I want to automatically download my database backup weekly. I'm using xampp on windows. I've google around and found that I've to make use of windows task scheduler and the action script in my console controller. I've read this and this and tried running the command from command prompt. I'm getting 'Unknown command'.

My code in console controller for now just for testing

<?php
namespace console\controllers;


use Yii;
use yii\console\Controller;

/**
 * Cron controller
 */
class TestController extends Controller {

    public function actionIndex() {
        echo "cron service runnning";
  }

}

And my main config file

<?php
$params = array_merge(
    require(__DIR__ . '/../../common/config/params.php'),
    require(__DIR__ . '/../../common/config/params-local.php'),
    require(__DIR__ . '/params.php'),
    require(__DIR__ . '/params-local.php')
);

return [
    'id' => 'app-console',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'console\controllers',
    'components' => [
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
         'authManager' => [
        'class' => 'yii\rbac\DbManager',
    ],
    ],

    'modules' => [
    'rbac' =>  [
        'class' => 'johnitvn\rbacplus\Module'
    ]       
],

    'params' => $params,
];

The error I'm getting enter image description here

like image 783
gojiraki Avatar asked Jan 28 '26 13:01

gojiraki


1 Answers

Make sure you have TestController.php file in console/controllers folder and its name is written correctly.

Then from project root folder you can call your command like that:

php yii test/index

or just:

php yii test

(because index is a default action (configured in $defaultAction) and you can omit it).

If you are outside of project folder, make sure to provide correct path:

php /path/to/project/folder/yii test
like image 98
arogachev Avatar answered Jan 30 '26 03:01

arogachev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!