This article mainly introduces the method of creating Cronjob timed task in Yii framework, and analyzes the related configuration, implementation steps and matters needing attention of Yii timing task with concrete instance form, and the friends who need can refer to
The example in this paper describes how the YII framework creates cronjob timed tasks. Share to everyone for your reference, as follows:
1. Add Environment Configuration
protected/config/console.php
<?phprequire_once (' env.php ');//The configuration for YIIC console application.//any writable Cconsoleapplica tion properties can be configured Here.return array ( ' BasePath ' =>dirname (FILE). Directory_separator. '. ', ' name ' = ' CMS Console ', //Application Components ' components ' =>array ( //main DB connection ' db ' =>array ( ' connectionString ' =>db_connection, ' username ' =>db_ USER, ' password ' =>db_pwd, ' enableparamlogging ' =>true, ' log ' =>array ( ' class ') = ' Clogrouter ', ' routes ' =>array ( Array ( ' class ' = ' Cfilelogroute ', ' levels ' = ') Error, warning ',),),),) ;
2. Adding a timed Task execution module
protected/commands/crons.php
<?phpdefined (' yii_debug ') or define (' Yii_debug ', true);//Including yiirequire_once ('/... /framework/yii.php ');//We'll use a separate config file$configfile= '/config/console.php ';//Creating and running Console Applicationyii::createconsoleapplication ($configFile)->run ();
3. Add a specific scheduled task
A timed task is usually a command-line program that derives from the Cconsolecommand class, such as
protected/commands/testcommand.php
Class Testcommand extends cconsolecommand{public function Run ($args) { //todo }}
4. Create Cronjob
0 * * * www php/path/to/crons.php Test >>/path/to/logs/test.log
5. Pass parameters to run ($params) in the scheduled task
0 * * * www php/path/to/crons.php Test param1 param2 ...