Home  >  Article  >  Backend Development  >  Yii framework creates scheduled task example through console command_PHP tutorial

Yii framework creates scheduled task example through console command_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:31:34801browse

Assume that the Yii project path is /home/apps/

1. Create the file /home/apps/protected/commands/crons.php

Copy code The code is as follows:

$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

Copy code The code is as follows:

return array(
'basePath'=> ;dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

'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



Copy code

The code is as follows:

class TestCommand extends CConsoleCommand { public function run() {
...
}
}



4. Create scheduled tasks



Copy code

The code is as follows:

$ crontab -e Insert


Copy code

The code is as follows:

1 * * * * /home/php/bin/php -f /home/ apps/protected/commands/crons.php Test &

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/763014.htmlTechArticleAssume the Yii project path is /home/apps/ 1. Create the file /home/apps/protected/commands/crons .php copy code as follows: ?php $yii = '/home/apps/framework/yii.php'; require_once($yi...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn