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:

  1. Create the commands/CleanController.php file with the following ...

Get Yii2 Application Development Cookbook - Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.