Creating console commands
Now that we know what built-in commands Yii2 provides, let's start adding our own commands. In Yii2, any custom commands we write are going to be stored in the /commands subfolder of our application. If this folder doesn't exist yet, go ahead and create it:
mkdir commands
Now, let's write a basic console command that just outputs some text:
- First, we'll create a new file called
BasicController.phpin thecommandsfolder:touch commands/BasicController.php - Now, let's write some PHP code. First, we need to declare the namespace that our
BasicControllerlives in. This namespace directly corresponds to thecontrollerNamespaceparameter we defined inconfig/console.php:<?php namespace app\commands;
- Then, we'll want to declare that we want to use the
\yii\console\Controllerclass in our new controller:use \yii\console\Controller;
- Next, we'll declare our controller class as follows:
class BasicController extends Controller { } - Finally,...