Yii -- Command task processing _ PHP Tutorial

Source: Internet
Author: User
Yii -- Command task processing. 1. configure the component task configuration file required to execute the task: the configuration method of protectedconfigconsole. php is similar to that of configuring the main file [html]? PhpThisistheconfigurationforyi 1. configure the components required to execute the task.
Task configuration file:/protected/config/console. php
The configuration method is similar to the configuration of the main file [html]

// This is the configuration for yiic console application.
// Any writable CConsoleApplication properties can be configured here.
Return array (
'Basepath' => dirname (_ FILE _). DIRECTORY_SEPARATOR .'..',
'Name' => 'My Console application ',
// Application components

// Automatically loaded models and component classes
'Import' => array (
'Application. models. * ', // load all model classes in the "application/models/" folder
'Application. components. * ', // load all application component classes in the "application/components/" folder
'Application. extensions. * ', // load all application component classes in the "application/extensions/" folder
),

'Components' => array (
// Uncomment the following to use a MySQL database
'DB' => array (
'Connectionstring' => 'MySQL: host = localhost; dbname = dbname', // connect to the mysql database
'Default' => true,
'Username' => 'root', // MySQL database username
'Password' => '000000', // MySQL database user password
'Charset' => 'utf8', // MySQL database encoding
'Tableprefix' => 'zd _ ', // MySQL database table prefix
'Enablesprofiling' => true,
'Enablesparamlogging' => true,
),
// Load the Email component
'Mailer' => array (
'Class' => 'application. extensions. mailer. EMailer ',
),
),
);

// This is the configuration for yiic console application.
// Any writable CConsoleApplication properties can be configured here.
Return array (
'Basepath' => dirname (_ FILE _). DIRECTORY_SEPARATOR .'..',
'Name' => 'My Console application ',
// Application components

// Automatically loaded models and component classes
'Import' => array (
'Application. models. * ', // load all model classes in the "application/models/" folder
'Application. components. * ', // load all application component classes in the "application/components/" folder
'Application. extensions. * ', // load all application component classes in the "application/extensions/" folder
),

'Components' => array (
// Uncomment the following to use a MySQL database
'DB' => array (
'Connectionstring' => 'MySQL: host = localhost; dbname = dbname', // connect to the mysql database
'Default' => true,
'Username' => 'root', // MySQL database username
'Password' => '000000', // MySQL database user password
'Charset' => 'utf8', // MySQL database encoding
'Tableprefix' => 'zd _ ', // MySQL database table prefix
'Enablesprofiling' => true,
'Enablesparamlogging' => true,
),
// Load the Email component
'Mailer' => array (
'Class' => 'application. extensions. mailer. EMailer ',
),
),
); 2. task file
Put it in the/protected/commands/file directory, inherit the CConsoleCommand base class, and name the task file as the task name + Command
For example, GoCommand. php [html]

/**
* Automatically run the file
*/
Class GoCommand extends CConsoleCommand
{


/**
* Endless loop output
*/
Public function run (){

For ($ I = 1; $ I> 0; $ I ++ ){
Self: echoWord ($ I );
Sleep (2); // sleep for 2 seconds

// Jump out
If (I = 500 ){
Break;
}
}
}

/**
* Output the hollo word
*/
Public function echoWord ($ I ){
Echo "Holo word -- $ I \ n ";
}
}

/**
* Automatically run the file
*/
Class GoCommand extends CConsoleCommand
{


/**
* Endless loop output
*/
Public function run (){

For ($ I = 1; $ I> 0; $ I ++ ){
Self: echoWord ($ I );
Sleep (2); // sleep for 2 seconds

// Jump out
If (I = 500 ){
Break;
}
}
}

/**
* Output the hollo word
*/
Public function echoWord ($ I ){
Echo "Holo word -- $ I \ n ";
}
} 3. execute the task
Open the command line tool and enter the/protected Directory of the project and enter the yiic command to display the prompt. the prompt list displays the task file you just wrote [html]
E: \ project \ app \ protected> yiic
Yii command runner (based on Yii v1.1.12)
Usage: E: \ zeee \ zyd \ protected \ yiic. php [Parameters...]

The following commands are available:
-Go
-Mailqueue
-Message
-Migrate
-Shell
-Webapp

To see inpidual command help, use the following:

E: \ project \ app \ protected> yiic
Yii command runner (based on Yii v1.1.12)
Usage: E: \ zeee \ zyd \ protected \ yiic. php [Parameters...]

The following commands are available:
-Go
-Mailqueue
-Message
-Migrate
-Shell
-Webapp

To see inpidual command help, use the following: execute the command yiic go To implement task processing

Refer task configuration file:/protected/config/console. php configuration method is similar to configuring the main file [html]? Php // This is the configuration for yi...

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.