《PHP應用:Yii框架創建cronjob定時任務的方法分析》要點:
本文介紹了PHP應用:Yii框架創建cronjob定時任務的方法分析,希望對您有用。如果有疑問,可以聯系我們。
相關主題:YII框架
PHP實戰本文實例講述了Yii框架創建cronjob定時任務的方法.分享給大家供大家參考,具體如下:
PHP實戰1. 添加環境配置
PHP實戰protected/config/console.php
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',
),
),
),
),
);
PHP實戰2. 添加定時任務執行模塊
PHP實戰protected/commands/crons.php
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();
PHP實戰3. 添加具體的定時任務
PHP實戰定時任務通常是一個命令行程序,從CConsoleCommand類派生,比如
protected/commands/TestCommand.php
PHP實戰
class TestCommand extends CConsoleCommand
{
public function run($args) {
//todo
}
}
PHP實戰4. 創建cronjob
PHP實戰
30 0 * * * www php /path/to/crons.php Test >>/path/to/logs/test.log
PHP實戰5. 傳入參數給定時任務中的run($params)
PHP實戰
30 0 * * * www php /path/to/crons.php Test param1 param2 ...
PHP實戰更多關于Yii相關內容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結》、《php優秀開發框架總結》、《smarty模板入門基礎教程》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
PHP實戰希望本文所述對大家基于Yii框架的PHP程序設計有所幫助.
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/746.html