Creating CLI commands
Yii has good command-line support and allows creating reusable console commands. Console commands are faster to create than web GUIs. If you need to create some kind of utility for your application that will be used by developers or administrators, console commands are the right tool.
To show how to create a console command, we'll create a simple command that will clean up various things, such as assets and temp directories.
Getting ready
Create a new yii2-app-basic
application using the composer, as described in the official guide at http://www.yiiframework.com/doc-2.0/guide-start-installation.html.
How to do it…
Carry out the following steps to create CLI commands:
Create the
commands/CleanController.php
file with the following code:<?php namespace app\commands; use yii\console\Controller; use yii\helpers\FileHelper; /** * Removes content of assets and runtime directories. */ class CleanController extends Controller { public $assetPaths = ['@app/web/assets']; ...