Yii Framework uses console commands to create scheduled task example _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.