Yii Framework official guide series 50-topics: console applications

Source: Internet
Author: User
Console applications are mainly used for offline operations required by online Web applications, such as code generation, search index compilation, and mail sending. the Yii Framework provides an object-oriented framework for compiling console applications. it allows the console...



Console applications are mainly used for offline operations required by online Web applications, such as code generation, search index compilation, and mail sending. the Yii Framework provides an object-oriented framework for compiling console applications. it allows console applications to access resources used by online Web applications (such as database connection information ).

1. Overview

Yii can run each console task in the form of a command line. The Console Command is a class inherited from CConsoleCommand.

When we use the yiic webapp tool to create an initialized Yii application skeleton, we can see the following two files in the protected folder:

  • Yiic: this is an executable script running on Linux/Unix;

  • Yiic. bat: this is an executable batch processing script running on Windows.

In the console window, enter the following command:

cd protectedyiic help

This will display a series of console commands. By default, these valid commands include those provided by the Yii Framework (system commands) and those compiled by the developer for independent applications (user commands ).

class SitemapCommand extends CConsoleCommand{    public function actionIndex($type, $limit=5) { ... }    public function actionInit() { ... }}

Then, the following Console Command will have results when calling actionIndex ('news', 5:

yiic sitemap index --type=News --limit=5// $limit takes default valueyiic sitemap index --type=News// $limit takes default value// because 'index' is a default action, we can omit the action nameyiic sitemap --type=News// the order of options does not matteryiic sitemap index --limit=5 --type=News

If an operation does not specify a value (e.g. -- type instead of -- type = News), the value of the corresponding action parameter is assumed to be true.

Note:The optional parameter format -- type News,-t News is not supported.

A parameter value can be an array (an array type prompt is required ):


public function actionIndex(array $types) { ... }

To use array values in command line parameters, simply repeat the same option:

yiic sitemap index --types=News --types=Article

The above Command will eventually call actionIndex (array ('news', 'article ')).

Yii also supports anonymous action parameters and global options starting from version 1.1.6.

Anonymous parameters indicate that these command line parameters are not displayed as options. for example, in the command yiic sitemap index -- limit = 5 News, we have an anonymous parameter and a named parameter limit with a value of 5.

To use anonymous parameters, a command action must be declared in the form of $ args. for example,


public function actionIndex($limit=10, $args=array()) {...}

$ Args array will load all anonymous parameter values.

Global options indicate options that can be shared by all actions in a command. for example, if multiple options are provided in a command, we may want each action to recognize an action called verbose. of course, we can declare the $ verbose parameter in every action method. a better way is to declare it as the command class.Public Member variableTo convert verbose to a global parameter:


class SitemapCommand extends CConsoleCommand{    public $verbose=false;    public function actionIndex($type) {...}}

The above code allows us to execute a command with the verbose option:

yiic sitemap index --verbose=1 --type=News
4. exit code

Note:The exit code feature in console commands is valid from version 1.1.11.

When using cronjob or using a continuously integrated server to automatically run console commands, either the command runs successfully or the command fails to run. you can check the exit code returned by the process.

These exit codes are integer values (this is the range in php world) from 0 to. 0 indicates that the exit is successful. Other non-0 values indicate that an error has occurred.

In an action method or the run () method of the console command, you can return an integer exit code when exiting. for example:


if (/* error */) {    return 1; // exit with error code 1}// ... do something ...return 0; // exit successfully

If no value is returned, the application exits and returns 0.

5. Custom console applications

By default, if an application is created using the yiic webapp tool, the configuration of the command line application will be placed in protected/config/console. php file. like a Web application configuration file, this file is a PHP script that returns the array of initialization configuration values of the console application instance. Therefore, any public attribute of CConsoleApplication can be configured in this file.

Because console commands are often created to serve Web applications, you need to access resources (such as database connections ). We can achieve this in the console configuration file as follows:


return array(    ......    'components'=>array(        'db'=>array(            ......        ),    ),);

As we can see, the configuration format is similar to that in our Web application, because the base classes of CConsoleApplication and CWebApplication are the same.

The above is the Yii Framework official guide series 50 -- special topic: Console Application Content. For more information, see PHP Chinese network (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.