Yii框架創(chuàng)建cronjob定時任務的方法分析
本文實例講述了Yii框架創(chuàng)建cronjob定時任務的方法。分享給大家供大家參考,具體如下:
1. 添加環(huán)境配置
protected/config/console.php
<?php
require_once('env.php');
// This is the configuration for yiic console application.
// Any writable CConsoleApplication properties can be configured here.
return array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'CMS Console',
// application components
'components'=>array(
//Main DB connection
'db'=>array(
'connectionString'=>DB_CONNECTION,
'username'=>DB_USER,
'password'=>DB_PWD,
'enableParamLogging'=>true,
),
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error, warning',
),
),
),
),
);
2. 添加定時任務執(zhí)行模塊
protected/commands/crons.php
<?php
defined('YII_DEBUG') or define('YII_DEBUG',true);
// including Yii
require_once('/../framework/yii.php');
// we'll use a separate config file
$configFile='/config/console.php';
// 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. 創(chuàng)建cronjob
30 0 * * * www php /path/to/crons.php Test >>/path/to/logs/test.log
5. 傳入?yún)?shù)給定時任務中的run($params)
30 0 * * * www php /path/to/crons.php Test param1 param2 ...
更多關于Yii相關內容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結》、《php優(yōu)秀開發(fā)框架總結》、《smarty模板入門基礎教程》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設計有所幫助。
相關文章
php筆記之:php函數(shù)range() round()和list()的使用說明
本篇文章介紹了,php函數(shù)range() round()和list()的使用說明。需要的朋友參考下2013-04-04
PHP使用mysqli同時執(zhí)行多條sql查詢語句的實例
今天小編就為大家分享一篇關于PHP使用mysqli同時執(zhí)行多條sql查詢語句的實例,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
PHP實現(xiàn)QQ、微信和支付寶三合一收款碼實例代碼
這篇文章主要給大家介紹了關于利用PHP如何實現(xiàn)QQ、微信和支付寶三合一收款碼的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-02-02

