Rumah  >  Artikel  >  pembangunan bahagian belakang  >  yii框架通过控制台命令创建定时任务步骤详解

yii框架通过控制台命令创建定时任务步骤详解

php中世界最好的语言
php中世界最好的语言asal
2018-05-21 09:41:491949semak imbas

这次给大家带来yii框架通过控制台命令创建定时任务步骤详解,yii框架通过控制台命令创建定时任务的注意事项有哪些,下面就是实战案例,一起来看一下。

假设Yii项目路径为 /home/apps/
1,创建文件 /home/apps/protected/commands/crons.php

<?php
$yii = &#39;/home/apps/framework/yii.php&#39;;
require_once
($yii); 
$configFile = 
dirname
(FILE).&#39;/../config/console.php&#39;;
Yii::createConsoleApplication($configFile)->run();

2,创建需要的配置文件 /home/apps/protected/config/console.php,配置需要的组件、数据库连接,日志等信息,格式类似主配置文件main.php。

<?php  return array(     &#39;basePath&#39;=>dirname(FILE).DIRECTORY_SEPARATOR.&#39;..&#39;,    &#39;name&#39;=>&#39;Emergency&#39;,    &#39;import&#39;=>array(              &#39;application.models.*&#39;,            &#39;application.components.*&#39;,             &#39;application.extensions.*&#39;,    ),    &#39;components&#39;=>array(        &#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;info, warning, error&#39;,                ),            ),        ),         &#39;db&#39;=>array(            &#39;class&#39;=>&#39;application.extensions.PHPPDO.CPdoDbConnection&#39;,            &#39;pdoClass&#39; => &#39;PHPPDO&#39;,            &#39;connectionString&#39; => &#39;mysql:host=xxxx;dbname=xxx&#39;,            &#39;emulatePrepare&#39; => true,            &#39;username&#39; => &#39;xxx&#39;,            &#39;password&#39; => &#39;xxx&#39;,            &#39;charset&#39; => &#39;utf8&#39;,            &#39;tablePrefix&#39; => &#39;tbl_&#39;,        ),    ), 
    &#39;params&#39; => require(&#39;params.php&#39;),);

3,在 /home/apps/protected/commands/ 下新建 TestCommand 类,继承 CConsoleCommand,在TestCommand中,可以使用项目的配置信息和Yii的各种方法。

<?php  
class TestCommand  
extends
 CConsoleCommand  
{  
    public function run()
    {  
        ...
    }  
}

4,创建定时任务
$ crontab -e
内容为:
1 * * * * /home/php/bin/php -f /home/apps/protected/commands/crons.php Test &即为每小时的第一分钟执行TestCommand类中的内容,类似的可以在/home/apps/protected/commands/下新建其他类,使用命令行执行。

有关crontab的用法,可以参考:

crontab命令基础与实例    crontab命令的一些例子    linux安装crontab详解    crontab学习笔记    学习linux设置定时任务的crontab命令    crontab 命令格式与例子    linux定时任务设置crontab学习    不错的crontab教程

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

thinkphp中volist标签使用案例详解

PHP使用Z字形顺序打印二叉树步骤详解

thinkphp中volist标签在ajax使用有哪些注意事项

Atas ialah kandungan terperinci yii框架通过控制台命令创建定时任务步骤详解. 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