Yii --Command 任務處理_PHP教程

來源:互聯網
上載者:User
1.配置,執行任務所需要的組件
任務設定檔:/protected/config/console.php
配置方法跟配置main檔案差不多[html]

// 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 MySQL database
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=dbname',//串連mysql資料庫
'emulatePrepare' => true,
'username' => 'root',//MySQL資料庫使用者名稱
'password' => '123456',//MySQL資料庫使用者密碼
'charset' => 'utf8',//MySQL資料庫編碼
'tablePrefix' => 'zd_', //MySQL資料庫表首碼
'enableProfiling'=>true,
'enableParamLogging'=>true,
),
//載入Email組件
'mailer' => array(
'class' => 'application.extensions.mailer.EMailer',
),
),
);

// 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 MySQL database
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=dbname',//串連mysql資料庫
'emulatePrepare' => true,
'username' => 'root',//MySQL資料庫使用者名稱
'password' => '123456',//MySQL資料庫使用者密碼
'charset' => 'utf8',//MySQL資料庫編碼
'tablePrefix' => 'zd_', //MySQL資料庫表首碼
'enableProfiling'=>true,
'enableParamLogging'=>true,
),
//載入Email組件
'mailer' => array(
'class' => 'application.extensions.mailer.EMailer',
),
),
);2.任務檔案
放在 /protected/commands/ 檔案目錄下繼承 CConsoleCommand 基類的為任務檔案 命名方法為 任務名稱+Command
例如 GoCommand.php[html]

/**
* 自動運行檔案
*/
class GoCommand extends CConsoleCommand
{


/**
* 死迴圈輸出
*/
public function run(){

for($i=1;$i>0;$i++){
self::echoWord($i);
sleep(2);//休眠2秒

//跳出
if(i==500){
break;
}
}
}

/**
* 輸出hollo word
*/
public function echoWord($i){
echo "hollo word --$i\n";
}
}

/**
* 自動運行檔案
*/
class GoCommand extends CConsoleCommand
{


/**
* 死迴圈輸出
*/
public function run(){

for($i=1;$i>0;$i++){
self::echoWord($i);
sleep(2);//休眠2秒

//跳出
if(i==500){
break;
}
}
}

/**
* 輸出hollo word
*/
public function echoWord($i){
echo "hollo word --$i\n";
}
}3.執行任務
開啟命令列工具,進入項目的/protected 目錄下 輸入yiic命令即出現提示,提示列表顯示剛才寫的任務檔案[html]
E:\project\app\protected>yiic
Yii command runner (based on Yii v1.1.12)
Usage: E:\zeee\zyd\protected\yiic.php [parameters...]

The following commands are available:
- go
- mailqueue
- message
- migrate
- shell
- webapp

To see individual command help, use the following:

E:\project\app\protected>yiic
Yii command runner (based on Yii v1.1.12)
Usage: E:\zeee\zyd\protected\yiic.php [parameters...]

The following commands are available:
- go
- mailqueue
- message
- migrate
- shell
- webapp

To see individual command help, use the following:執行命令 yiic go 可實現任務處理

http://www.bkjia.com/PHPjc/477288.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477288.htmlTechArticle1.配置,執行任務所需要的組件 任務設定檔:/protected/config/console.php 配置方法跟配置main檔案差不多[html] ?php // This is the configuration for yi...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.