Home > Article > Backend Development > Yii --Command task processing_PHP tutorial
1. Configure the components required to perform tasks
Task configuration file:/protected/config/console.php
The configuration method is similar to configuring 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 model and component classes
'import'=>array(
'application.models.*',//Load all model classes under 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
'emulatePrepare' => true,
‘username’ => ‘root’,//MySQL database username
'password' => '123456',//MySQL database user password
'charset' => 'utf8',//MySQL database encoding
'tablePrefix' => 'zd_', //MySQL database table prefix
'enableProfiling'=>true,
'enableParamLogging'=>true,
),
//Load the Email component
'mailer' => array(
'Class' = & gt; 'application.extersions.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 model 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 mysql database
'emulatePrepare' => true,
'username' => 'root',//MySQL database username
'password' => '123456',//MySQL database user password
'charset' => 'utf8',//MySQL database encoding
'tablePrefix' => 'zd_', //MySQL database table prefix
'enableProfiling'=>true,
‘enableParamLogging’=>true,
),
//Load Email component
'mailer' => array(
'class' => 'application.extensions.mailer.EMailer',
),
),
);2.Task file
Place it in the /protected/commands/ file directory and inherit the CConsoleCommand base class as a task file. The naming method is Task name+Command
For example GoCommand.php[html]
/**
* Automatically run files
*/
class GoCommand extends CConsoleCommand
{
/**
* * Infinite 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 hollo word
*/
Public function echoWord($i){
echo "hollo word --$in";
}
}
/**
* Automatically run files
*/
class GoCommand extends CConsoleCommand
{
* Infinite 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;
}
}
}
/**
*/
public function echoWord($i){
echo "hollo word --$in";
}
}3.Perform tasks
Open the command line tool, enter the /protected directory of the project, enter the yiic command, and a prompt will appear. The prompt list displays the task file just written [html]
E:projectappprotected>yiic
Yii command runner (based on Yii v1.1.12)
Usage: E:zeeezydprotectedyiic.php
The following commands are available:
- go
- mailqueue
- message
- migrate
- shell
- webapp
To see individual command help, use the following:
E:projectappprotected>yiic
Usage: E:zeeezydprotectedyiic.php
The following commands are available:
- mailqueue
- message
- migrate
- shell
- webapp
To see individual command help, use the following:Execute the command yiic go to achieve task processing