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

创建任务文件夹

在相关模块中创建command文件夹,与controller/model/view目录同级

 

创建任务文件

在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)
    {
        
    }
}

注册任务

在command.php中添加你所创建的任务文件的路径

 

命令行中执行

任务文件中修改执行方法

    /**
     * 业务逻辑
     * @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

输出:

设置、获取参数

使用场景:当同一个任务文件执行多个不同任务时

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

执行:

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JSON_L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值