Yii framework uses console commands to create a scheduled task example _ php instance

Source: Internet
Author: User
This article mainly introduces the example of creating a scheduled task using commands in the yii framework through the console. If you need a scheduled task, refer to the following command to assume 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.

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.