Console Application Example in Yii2

<note>This is based on Yii2 Advanced Application template</note>

The console/config/main.php config file has the line 'controllerNamespace' ⇒ 'console\controllers' which includes the everything in the console/controllers directory to be included in the console application.

Steps

Create a file called TestController.php in console/controllers as below

<?php
 
namespace console\controllers;
use yii\console\Controller;
 
/**
 * Test controller
 */
class TestController extends Controller {
 
    public function actionIndex() {
        echo date( 'Y-m-d H:i:s');
        echo "\n";
    }
 
    public function actionYou($you = NULL) {
        if (empty($you))
          echo "Hello You!";
        else
          echo "Hello " . $you;
        echo "\n";
    }
 
}

Now run yii in the main application directory as

./yii test
./yii test/you
./yii test/you Harry

QR Code
QR Code tech:yii2:console_application (generated for current page)