Also available in these languages:
DeutschEnglishEspañolFrançaisBahasa Indonesia日本語polskiPortuguêsRomâniaРусскийsvenska简体中文

Console Applications

Console applications are mainly used by a Web application to perform offline work, such as code generation, search index compiling, email sending, etc. Yii provides a framework for writing console applications in an object-oriented and systematic way.

Yii represents each console task in terms of a command, and a console application instance is used to dispatch a command line request to an appropriate command. The application instance is created in an entry script. To execute a console task, we simply run the corresponding command on the command line as follows,

php entryScript.php CommandName Param0 Param1 ...

where CommandName refers to the command name which is case-insensitive, and Param0, Param1 and so on are parameters to be passed to the command instance.

The entry script for a console application is usually written like the following, similar to that in a Web application,

defined('YII_DEBUG') or define('YII_DEBUG',true);
// include Yii bootstrap file
require_once('path/to/yii/framework/yii.php');
// create application instance and run
$configFile='path/to/config/file.php';
Yii::createConsoleApplication($configFile)->run();

We then create command classes which should extend from CConsoleCommand. Each command class should be named as its command name appended with Command. For example, to define an email command, we should write an EmailCommand class. All command class files should be placed under the commands subdirectory of the application base directory.

Tip: By configuring CConsoleApplication::commandMap, one can also have command classes in different naming conventions and located in different directories.

Writing a command class mainly involves implementing the CConsoleCommand::run method. Command line parameters are passed as an array to this method. Below is an example:

class EmailCommand extends CConsoleCommand
{
    public function run($args)
    {
        $receiver=$args[0];
        // send email to $receiver
    }
}

At any time in a command, we can access the console application instance via Yii::app(). Like a Web application instance, console application can also be configured. For example, we can configure a db application component to access the database. The configuration is usually specified as a PHP file and passed to the constructor of the console application class (or createConsoleApplication in the entry script).

Using the yiic Tool

We have used the yiic tool to create our first application. The yiic tool is in fact implemented as a console application whose entry script file is framework/yiic.php. Using yiic, we can accomplish tasks such as creating a Web application skeleton, generating a controller class or model class, generating code needed by CRUD operations, extracting messages to be translated, etc.

We can enhance yiic by adding our own customized commands. To do so, we should start with a skeleton application created using yiic webapp command, as described in Creating First Yii Application. The yiic webapp command will generate two files under the protected directory: yiic and yiic.bat. They are the local version of the yiic tool created specifically for the Web application.

We can then create our own commands under the protected/commands directory. Running the local yiic tool, we will see our own commands appearing together with the standard ones. We can also create our own commands to be used in the yiic shell tool. To do so, just drop our command class files under the protected/commands/shell directory.

Starting from version 1.1.1, we can also create global commands that can be shared by all Yii applications on the same machine. To do so, define an environment variable named YII_CONSOLE_COMMANDS which should point to an existing directory. We then put our global command class files under this directory, and we will see these commands become available wherever we use the yiic tool.

$Id: topics.console.txt 1870 2010-03-09 22:23:19Z qiang.xue $
If you found any typos or errors in the tutorial, please create a Yii ticket to report it. If it is a translation error, please create a Yiidoc ticket, instead. Thank you.

Total 6 comments:

#156
Command running another commands
by rattus at 3:15am on March 31, 2009.

Hi, is there an easy way of running commands from another command ?

for example i want to create command that will run "model <db_table>" in yiic shell for list of db tables.

#204
Help?
by kevinkorb at 4:21am on April 18, 2009.

I tried implementing this, however I got the error: exception 'CException' with message 'Property "CConsoleApplication.defaultController" is not defined.' in /data/web/vhosts/kevinkorb.com/framework/base/CComponent.php:154

Now I created a file protected/commands/ParseRssCommand.php <?php class ParseRssCommand extends CConsoleCommand { public function run($args) { $receiver=$args0; echo "Hello world!"; } }

Then created my entry.php file. <?php defined('YII_DEBUG') or define('YII_DEBUG',true); // include Yii bootstrap file require_once('framework/yii.php'); // create application instance and run $configFile='protected/config/main.php'; Yii::createConsoleApplication($configFile)->run();

Now when I go to run this I get the error. kevin-korbs-macbook:kevinkorb.com kevinkorb$ php entry.php ParseRssCommand exception 'CException' with message 'Property "CConsoleApplication.defaultController" is not defined.' in /data/web/vhosts/kevinkorb.com/framework/base/CComponent.php:154 Stack trace:

0 /data/web/vhosts/kevinkorb.com/framework/base/CModule.php(404): CComponent->__set('defaultControll...', 'post')

1 /data/web/vhosts/kevinkorb.com/framework/base/CApplication.php(116): CModule->configure(Array)

2 /data/web/vhosts/kevinkorb.com/framework/YiiBase.php(88): CApplication->__construct('protected/confi...')

3 /data/web/vhosts/kevinkorb.com/entry.php(7): YiiBase::createConsoleApplication('protected/confi...')

4 {main}kevin-korbs-macbook:kevinkorb.com kevinkorb$ php entry.php ParseRssCom1exception 'CException' with message 'Property "CConsoleApplication.defaultController" is not defined.' in /data/web/vhosts/kevinkorb.com/framework/base/CComponent.php:154

Stack trace:

0 /data/web/vhosts/kevinkorb.com/framework/base/CModule.php(404): CComponent->__set('defaultControll...', 'post')

1 /data/web/vhosts/kevinkorb.com/framework/base/CApplication.php(116): CModule->configure(Array)

2 /data/web/vhosts/kevinkorb.com/framework/YiiBase.php(88): CApplication->__construct('protected/confi...')

3 /data/web/vhosts/kevinkorb.com/entry.php(7): YiiBase::createConsoleApplication('protected/confi...')

What did I do wrong? Thanks.

#205
Oops, apparantly I can't edit that either...
by kevinkorb at 4:22am on April 18, 2009.

I'm sure you can fix that for me....

Thanks.

#206
Nevermind, I figured it out.
by kevinkorb at 4:32am on April 18, 2009.

You pass the console.php configuration and not the main.php configuration.

Thanks.

#208
Next issue
by kevinkorb at 4:07am on April 18, 2009.

The autoloader doesn't want to autoload my models to be used in my console application. What do I need to do for this to work?

#371
Re #208
by sAe at 6:02pm on June 9, 2009.

Well did you import them in your config file? console.php?

Your Comment:

You may enter comment using Markdown syntax.

Please login with your forum account.
Note: you must have at least ONE forum post with your account.