今天打算在phalcon項目下加入一個定時任務,來發送郵件。php
看了官網的例子,配置和運行都少量雜亂,還有個bug,見備註, 我這邊主要講解如何將command line 的配置獨立出來,若是初上手的同窗,建議先運行官網第一個例子,成功後,再實踐一下官網第二個例子,出現報錯,就看我備註,而後再看我這如何將配置獨立處理。html
借鑑思想:主要借鑑Yii框架中,相似功能commands的配置console.php。數據庫
第一步:在app下的tasks文件夾下建三個文件:app
cl_config.php //直接把app/config/config.php內容copy進去框架
cl_loader.php //一樣把app/config/loader.php內容copy進去,注意要有所取捨,好比modelsDir,用數據庫時用,但controllersDir就沒啥用,能夠註釋掉spa
cl_services.php //也一樣copy進去,但$di不須要從新生成,直接用好了,把沒用的都註釋掉,通常只保留$di->set('db'.... 那段命令行
第二步:修改app下的cli.php文件htm
if(is_readable(APPLICATION_PATH . '/tasks/cl_config.php')) { //導入cl_config.php
$config = include APPLICATION_PATH . '/tasks/cl_config.php';
$di->set('config', $config);
}
include APPLICATION_PATH. "/tasks/cl_loader.php"; //如今引入autoloader,
include APPLICATION_PATH. "/tasks/cl_services.php"; //引入cl_servicesstring
ok啦,,,,it
備註:
cli.php裏的一部分配置
$arguments = array();
foreach($argv as $k => $arg) {
if($k == 1) {
$arguments['task'] = $arg;
} elseif($k == 2) {
$arguments['action'] = $arg;
} elseif($k >= 3) {
$arguments[] = $arg; //需改爲$arguments['params'][] = $arg;不然,在命令行帶上參數運行時,會報錯,以下
}
}
運行:$ php app/cli.php main test ff ss ww mm
報錯內容:
PHP Catchable fatal error: Argument 1 passed to MainTask::testAction() must be of the type array, string given in /Users/liangzhongyuan/Sites/library/app/tasks/MainTask.php on line 24
Catchable fatal error: Argument 1 passed to MainTask::testAction() must be of the type array, string given in /Users/liangzhongyuan/Sites/library/app/tasks/MainTask.php on line 24
相關鏈接:
http://www.myleftstudio.com/reference/cli.html //官網講解