本文實例講述了Yii實現(xiàn)Command任務處理的方法。分享給大家供大家參考,具體如下:
1.配置,執(zhí)行任務所需要的組件
任務配置文件:/protected/config/console.php
配置方法跟配置main文件差不多
?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 = My Console Application , // application components // 自動載入的模型和組件類 import = array( application.models.* ,//載入 application/models/ 文件夾下的所有模型類 application.components.* ,//載入 application/components/ 文件夾下的所有應用組件類 application.extensions.* ,//載入 application/extensions/ 文件夾下的所有應用組件類 components = array( // uncomment the following to use a html' target='_blank'>MySQL database db = array( connectionString = mysql:host=localhost;dbname=dbname ,//連接mysql數(shù)據(jù)庫 emulatePrepare = true, username = root ,//MySQL數(shù)據(jù)庫用戶名 password = 123456 ,//MySQL數(shù)據(jù)庫用戶密碼 charset = utf8 ,//MySQL數(shù)據(jù)庫編碼 tablePrefix = zd_ , //MySQL數(shù)據(jù)庫表前綴 enableProfiling = true, enableParamLogging = true, //加載Email組件 mailer = array( class = application.extensions.mailer.EMailer ,);
2.任務文件
放在 /protected/commands/ 文件目錄下繼承 CConsoleCommand 基類的為任務文件 命名方法為 任務名稱+Command
例如 GoCommand.php
?php * 自動運行文件class GoCommand extends CConsoleCommand * 死循環(huán)輸出 public function run(){ for($i=1;$i $i++){ self::echoWord($i); sleep(2);//休眠2秒 //跳出 if(i==500){ break; * 輸出hollo word public function echoWord($i){ echo hollo word --$i/n }
3.執(zhí)行任務
打開命令行工具,進入項目的/protected 目錄下 輸入yiic命令即出現(xiàn)提示,提示列表顯示剛才寫的任務文件
E:/project/app/protected yiicYii command runner (based on Yii v1.1.12)Usage: E:/zeee/zyd/protected/yiic.php command-name [parameters...]The following commands are available:- mailqueue- message- migrate- shell- webappTo see inpidual command help, use the following:
執(zhí)行命令 yiic go 可實現(xiàn)任務處理
以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP !
相關推薦:
關于Yii中CDBCriteria的常用方法
如何解決Yii2下session跨域名共存的問題
以上就是如何通過Yii實現(xiàn)Command任務處理的方法的詳細內容,PHP教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。
新聞熱點
疑難解答