Console Applications

Console applications are mainly used by a Web application to performoffline work, such as code generation, search index compiling, emailsending, etc. Yii provides a framework for writing console applications inan object-oriented and systematic way.

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

  1. 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 commandinstance.

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

  1. defined('YII_DEBUG') or define('YII_DEBUG',true);
  2. // include Yii bootstrap file
  3. require_once('path/to/yii/framework/yii.php');
  4. // create application instance and run
  5. $configFile='path/to/config/file.php';
  6. 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 withCommand. For example, to define an email command, we should write anEmailCommand class. All command class files should be placed under thecommands subdirectory of the application basedirectory.

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 theCConsoleCommand::run method. Command line parameters are passed as anarray to this method. Below is an example:

  1. class EmailCommand extends CConsoleCommand
  2. {
  3. public function run($args)
  4. {
  5. $receiver=$args[0];
  6. // send email to $receiver
  7. }
  8. }

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

1. Using the yiic Tool

We have used the yiic tool to create our firstapplication. The yiic tool is in factimplemented as a console application whose entry script file isframework/yiic.php. Using yiic, we can accomplish tasks such ascreating a Web application skeleton, generating a controller class or modelclass, generating code needed by CRUD operations, extracting messages to betranslated, etc.

We can enhance yiic by adding our own customized commands. To do so, weshould start with a skeleton application created using yiic webappcommand, as described in Creating First YiiApplication. The yiic webapp commandwill generate two files under the protected directory: yiic andyiic.bat. They are the local version of the yiic tool createdspecifically for the Web application.

We can then create our own commands under the protected/commandsdirectory. Running the local yiic tool, we will see that our own commandsappearing together with the standard ones. We can also create our owncommands to be used when yiic shell is used. To do so, just drop ourcommand class files under the protected/commands/shell directory.

原文: https://www.yiichina.com/doc/guide/1.0/topics.console