tp6使用cli(cmd)命令行模式调用访问控制器

博客介绍了如何在ThinkPHP6(简称tp6)中通过命令行CLI模式调用控制器,实现类似功能。作者提到tp6默认不支持直接通过CLI访问控制器,但通过自定义Command可以解决这个问题。文章详细讲解了实现过程,包括命令配置、参数传递和执行方法。虽然存在大小写敏感的问题,但调试方便且只需一次性配置。
摘要由CSDN通过智能技术生成

tp6使用cli(cmd)命令行模式调用访问控制器

因为thinkphp6 简称tp6,默认不支持直接cli命令行模式访问控制器,于是利用官方的command实现了访问控制器.

优点如下:

1、调试方便,能准确的抛出各种异常(顺便吐槽下tp6框架的命令行基本上是个鸡肋,远远达不到laravel/yii2的易用级别。)

2、无需重复配置命令行,基本上配一次后,让tp6命令行达到与控制器一样方便使用的级别

不足点:

class与function大小写需要严格区分,对于洁癖程序猿可能是优点,如果你不想注意大小写,class与function简单粗暴用小写就行。

实现效果如下 ,支持多模块:

php think action 模块/控制器/方法
php执行命令 tp6框架命令行入口文件 编写的action方法 模块/控制器/方法

可支持参数比如

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

Action代码如下

<?php
declare (strict_types=1);

namespace app\common\controller;

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

在config/console.php中,添加

'commands' => [
    "action"=>"\app\common\controller\Action",
],

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值