ThinkPHP5.1定时任务设置及传参

                        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;
        }
    }
}

执行:

输出为不同的内容,可用来执行不同的业务逻辑,本文章到此已经结束!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章