Failed to start console controller

I’m trying to write a simple cron console controller based on some samples I found.
This is my controller code:

namespace console\controllers;

use Yii;
use yii\console\Controller;

class CronController extends Controller
    public function actionIndex()
        echo "hello";

this is my main.php config file:

return [
    'id' => 'app-console',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'console\controllers',
    'controllerMap' => [
        'cron' => [
            'class' => 'console\controllers\CronController',
    'aliases' => [
        'console' =>  '/console',

and this is my yii script:

defined('YII_DEBUG') or define('YII_DEBUG', true);

require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/vendor/yiisoft/yii2/Yii.php';

$config = require __DIR__ . '/console/config/main.php';

$application = new yii\console\Application($config);

$exitCode = $application->run();

When running php yii cron I get the following error:

Exception ‘ReflectionException’ with message ‘Class console\controllers\CronController does not exist’

any idea what I’m doing wrong ?

Could be mismatch of file path vs namespace.

the class is in console/controllers folder and I’m running yii command from its parent folder

any other suggestion ? I’m struggling with this issue for few days already and it seems like a very basic functionality that should work.
digging into the code it seems the yii looks for controller only under yiisoft/yii2/console/controllers/ so when I put my controller there it works, but I assume it should know to find it also under console/controller, obviously I don’t want to put my controller inside yii internal folder.

Do I understand correctly that this is your directory structure?


If so, I suspect basepath is not correct. dirname(__DIR__) should be dirname(__DIR__, 2)

yes this the my directory structure.
actually using dirname(DIR, 2), didn’t work for me.
what eventually work for me was changing alias to:

'aliases' => [				
    '@console' => __DIR__ . '/console',        

not sure if this is the proper solution and if there’s another way to make it works, but I already spent a lot of time on it so I guess I’ll keep it like that for now.

Thanks for the help.