The yii Framework uses console commands to create scheduled task examples. Assume that the Yii project path is homedesk1. the file homeappsprotectedcommandscrons. php is copied using the following code :? Php $ yiihome‑frameworkyii. php; require_once ($ yi assumes that the Yii project path is/home/apps/
1. create a file/home/apps/protected/commands/crons. php
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 to configure the required components, database connections, logs, and other information. the format is similar to the main. php configuration file.
The code is as follows:
Return array (
'Basepath' => dirname (_ FILE _). DIRECTORY_SEPARATOR .'..',
'Name' => 'ergency ',
'Import' => array (
'Application. models .*',
'Application. components .*',
'Application. extensions .*',
),
'Components' => array (
'Log' => array (
'Class' => 'clogrouter ',
'Routes '=> array (
Array (
'Class' => 'cfilelogroute ',
'Levels' => 'info, warning, error ',
),
),
),
'DB' => array (
'Class' => 'application. extensions. PHPPDO. cpdodbconnection ',
'Pdoclass' => 'phppdo ',
'Connectionstring' => 'MySQL: host = xxxx; dbname = xxx ',
'Default' => true,
'Username' => 'XXX ',
'Password' => 'XXX ',
'Charset' => 'utf8 ',
'Tableprefix' => 'tbl _',
),
),
'Params' => require ('params. php '),
);
3. create a new TestCommand class under/home/apps/protected/commands/and inherit from CConsoleCommand. in TestCommand, you can use the project configuration information and various Yii methods.
The code is as follows:
Class TestCommand extends CConsoleCommand
{
Public function run ()
{
...
}
}
4. create a scheduled task
The code is as follows:
$ Crontab-e
Insert
The code is as follows:
1 ***/home/php/bin/php-f/home/apps/protected/commands/crons. php Test &
That is, the content in the TestCommand class is executed in the first minute of every hour. Similarly, you can create other classes under/home/apps/protected/commands/and execute them using the command line.
Examples/home/apps/1. create a file/home/apps/protected/commands/crons. php with the following code :? Php $ yii = '/home/apps/framework/yii. php'; require_once ($ yi...