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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.