Rumah  >  Artikel  >  pembangunan bahagian belakang  >  在Yii中创建cronjob定时任务的实例详解

在Yii中创建cronjob定时任务的实例详解

巴扎黑
巴扎黑asal
2018-05-17 10:54:381357semak imbas

这篇文章主要介绍了Yii框架创建cronjob定时任务的方法,结合具体实例形式分析了Yii定时任务相关配置、实现步骤与注意事项,需要的朋友可以参考下

本文实例讲述了Yii框架创建cronjob定时任务的方法。分享给大家供大家参考,具体如下:

1. 添加环境配置

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. 添加定时任务执行模块

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. 添加具体的定时任务

定时任务通常是一个命令行程序,从CConsoleCommand类派生,比如
protected/commands/TestCommand.php

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

4. 创建cronjob

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

5. 传入参数给定时任务中的run($params)

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

Atas ialah kandungan terperinci 在Yii中创建cronjob定时任务的实例详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn