Home > Article > Backend Development > Yii framework creates scheduled task example through console command_PHP tutorial
Assume that the Yii project path is /home/apps/
1. Create the file /home/apps/protected/commands/crons.php
$yii = '/home/apps/framework/yii.php';
require_once($yii);
$configFile = dirname(__FILE__).'/../config/console.php';
Yii::createConsoleApplication($configFile)->run();
2. Create the required configuration file /home/apps/protected/config/console.php, configure the required components, database connections, logs and other information. The format is similar to the main configuration file main.php
'name'=>'Emergency',
'import'=>array(
'application.models.*',
'application.components.*',
'application.extensions.*',
),
'components'=>array(
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
) array (
'class'=>'CFileLogRoute', ),
),
‘ db '= & gt; array (
' class' = & gt; 'application.extensions.phppdo.cpdodbconnection',
'pdoclass' = & gt;' pHppdo ',
'ConnectionString' = & GT; 'MySQL ; 'charset' => 'utf8',
'tablePrefix' => 'tbl_',
),
),
'params' => require('params. php'),
);
3. Create a new TestCommand class under /home/apps/protected/commands/ and inherit CConsoleCommand. In TestCommand, you can use the project’s configuration information and Yii’s various methods
The code is as follows:
The code is as follows:
The code is as follows:
That is, the contents of the TestCommand class are executed in the first minute of every hour. Similarly, other classes can be created under /home/apps/protected/commands/ and executed using the command line.