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.


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

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!";
          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)