ThinkPHP5.1定時任務設置及傳參
1.在相關模塊中創建command文件夾,與controller/model/view目錄同級
2.在command中創建任務文件
<?php
namespace app\index\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class CronImage extends Command
{
protected function configure()
{
//setName與文件名稱保持一致 setDEscription則是說明
$this->setName('CronImage')->setDescription("設置圖片表測試數據");
}
/**
* 業務邏輯
* @param Input $input
* @param Output $output
* @return int|void|null
*/
protected function execute(Input $input, Output $output)
{
}
}
3.在command.php中添加你所創建的任務文件的路徑
4.命令行中執行
任務文件中修改執行方法
/**
* 業務邏輯
* @param Input $input
* @param Output $output
* @return int|void|null
*/
protected function execute(Input $input, Output $output)
{
$output->writeln('type value');
echo 1111;
}
然後命令行 執行以下命令
php think CronImage
輸出:
5.設置參數及獲取參數(當同一個任務文件執行多個不同任務時)
<?php
namespace app\index\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
class CronImage extends Command
{
protected function configure()
{
$this->addArgument('type', Argument::REQUIRED); //必傳參數
//$this->addArgument('type', Argument::OPTIONAL);//可選參數
$this->setName('CronImage')->setDescription("設置圖片表測試數據");//setName與文件名稱保持一致 setDEscription則是說明
}
/**
* 業務邏輯
* @param Input $input
* @param Output $output
* @return int|void|null
*/
protected function execute(Input $input, Output $output)
{
$output->writeln('type value');
$type = $input->getArgument('type');
switch($type)
{
case 1:
echo 1;
break;
case 2:
echo '2-';
break;
}
}
}
執行:
輸出爲不同的內容,可用來執行不同的業務邏輯,本文章到此已經結束!