thinkphp5、thinkphp6命令行command执行控制器方法

8 篇文章 0 订阅

最近做数据采集时总会遇到接口执行一段时间之后请求时间过长报404,命令行执行的完美解决这一问题,但之前没做过命令行执行thinkphp方法,找了好多资料,踏了不少坑,终于找到解决方法,下面将代码贴在下面

特别鸣谢这一大神

原文地址:tp5和tp6使用cli(cmd)模式调用控制器 | 时刻需 

tp5

1.command代码如下

<?php
/**
 * Created by 大师兄
 * 派系: 神秘剑派
 * 技能: zxc
 * Date: 2021/12/13
 * Time: 15:58
 * QQ:  997823131
 * 网址: https://www.shikexu.com
 */
namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Action extends Command{

    protected function configure()
    {
        $this->setName("action")
            ->addArgument("route",Argument::OPTIONAL,"请输入路由地址")//路由地址必须输入
            ->addOption('option','o',Option::VALUE_REQUIRED,'设置参数')
            ->setDescription("command 模式来运行控制器方法");
    }

    protected function execute(Input $input, Output $output)
    {
        $Argument = $input->getArguments();
        if($Argument['command'] == 'action'){
            if($input->hasOption('option')){
                $result = action($this->route($Argument['route']),$input->getOption('option'));
                $output->writeln($result);
            }else{
                $result = action($this->route($Argument['route']));
                $output->writeln($result);
            }
        }

    }

    public function route($route = ''){
        if($route){
            $route = explode("/",$route);
            $module = isset($route[0])?$route[0]:"index";
            $controller = isset($route[1]) ? $route[1] : 'index';
            $action = isset($route[2]) ? $route[2] : 'index';
            return $module . '/' . $controller . '/' . $action;
        }
        return $route;
    }
}

2.然后在application/command.php中添加

"action"=>"app\common\command\Action"

3.使用案例

php think action common/test/index

tp6

1.command代码如下

<?php
/**
 * Created by 大师兄
 * 派系: 神秘剑派
 * 技能: zxc
 * Date: 2021/12/13
 * Time: 15:58
 * QQ:  997823131
 * 网址: https://www.shikexu.com
 */
namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\Exception;

class Action extends Command{

    protected function configure()
    {
        $this->setName("action")
            ->addArgument("route",Argument::OPTIONAL,"your run route path!")//路由地址必须输入
            ->addOption('option','o',Option::VALUE_REQUIRED,'set Controller Argument')
            ->setDescription("Command run Controller Action !");
    }

    protected function execute(Input $input, Output $output)
    {

        $Argument = $input->getArguments();
        if($Argument['command'] == 'action'){
            if($input->hasOption('option')){
                $params = $this->option($input->getOption('option'));
                $class_fun = $this->route($Argument['route']);
                $fun = $class_fun['fun'];
                $result = app("{$class_fun['app']}\\{$class_fun['module']}\\{$class_fun['controller']}\\{$class_fun['class']}")->$fun($params);
                $output->writeln($result);
            }else{


                $class_fun = $this->route($Argument['route']);
                // throw new Exception("类".$class_fun['class']);
                // throw new Exception("方法".$class_fun['fun']);
                $fun = $class_fun['fun'];
                $result = app("{$class_fun['app']}\\{$class_fun['module']}\\{$class_fun['controller']}\\{$class_fun['class']}")->$fun();
                $output->writeln($result);
            }
        }

    }

    public function route($route = ''){
        // throw new Exception("路由是".$route);

        $class_fun['class'] = "app\\index\\controller\\index";
        $class_fun['fun'] = "index";

        if($route){
            $app = "app";
            $controller = "controller";
            $route = explode("/",$route);
            $module = isset($route[0])?$route[0]:"index";
            $class = isset($route[1]) ? $route[1] : 'index';
            $fun = isset($route[2]) ? $route[2] : 'index';

            // $class_fun['class'] = $app."\\".$module."\\".$controller."\\".$class;
            $class_fun['class'] = $class;
            $class_fun['fun'] = $fun;
            $class_fun['module'] = $module;
            $class_fun['app'] = $app;
            $class_fun['controller'] = $controller;


        }
        return $class_fun;
    }

    public function option($option){

        /* 整理成数组 start */
        $params = array();
        $option_arr = explode(",",$option);
        foreach ($option_arr as $key=>$val){
            $temp_params = explode("=",$val);
            $params[$temp_params[0]] = $temp_params[1];
        }
        /* 整理成数组 end */
        return $params;
    }
}

2.在config/sonsole.php中,添加

"action"=>"app\common\command\Action",

3.使用案例

php think action common/test/index --option name=jack,age=10

对应控制器方法

public function index($params=array()){


        print_r($params);
        print_r("cmd输出\n");
        return "结果";

    }

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值