Home  >  Article  >  Backend Development  >  PHP example-Yii framework method analysis of creating cronjob scheduled tasks

PHP example-Yii framework method analysis of creating cronjob scheduled tasks

微波
微波Original
2017-06-28 13:08:151567browse

This article mainly introduces the method of creating cronjob scheduled tasks in Yii framework, and analyzes the related configuration, implementation steps and precautions of Yii scheduled tasks in the form of specific examples. Friends in need can refer to the following

The example of this article describes the method of creating cronjob scheduled tasks in Yii framework. Share it with everyone for your reference, the details are as follows:

1. Add environment configuration

protected/config/console.php

<?php
require_once(&#39;env.php&#39;);
// This is the configuration for yiic console application.
// Any writable CConsoleApplication properties can be configured here.
return array(
  &#39;basePath&#39;=>dirname(FILE).DIRECTORY_SEPARATOR.&#39;..&#39;,
  &#39;name&#39;=>&#39;CMS Console&#39;,
  // application components
  &#39;components&#39;=>array(
    //Main DB connection
    &#39;db&#39;=>array(
      &#39;connectionString&#39;=>DB_CONNECTION,
      &#39;username&#39;=>DB_USER,
      &#39;password&#39;=>DB_PWD,
      &#39;enableParamLogging&#39;=>true,
    ),
    &#39;log&#39;=>array(
      &#39;class&#39;=>&#39;CLogRouter&#39;,
      &#39;routes&#39;=>array(
        array(
          &#39;class&#39;=>&#39;CFileLogRoute&#39;,
          &#39;levels&#39;=>&#39;error, warning&#39;,
        ),
      ),
    ),
  ),
);

2. Add a scheduled task execution module

protected/commands/crons.php

<?php
defined(&#39;YII_DEBUG&#39;) or define(&#39;YII_DEBUG&#39;,true);
// including Yii
require_once(&#39;/../framework/yii.php&#39;);
// we&#39;ll use a separate config file
$configFile=&#39;/config/console.php&#39;;
// creating and running console application
Yii::createConsoleApplication($configFile)->run();

3. Add a specific scheduled task

Timing The task is usually a command line program, derived from the CConsoleCommand class, such as

protected/commands/TestCommand.php

class TestCommand extends CConsoleCommand
{
  public function run($args) {
    //todo
  }
}

4. Create a cronjob

30 0 * * * www php /path/to/crons.php Test >>/path/to/logs/test.log

5. Pass in the parameters to run($params) in the scheduled task

30 0 * * * www php /path/to/crons.php Test param1 param2 ...

The above is the detailed content of PHP example-Yii framework method analysis of creating cronjob scheduled tasks. For more information, please follow other related articles on the PHP Chinese website!

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