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


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

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

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

    php think action  模块/控制器/方法

    1.command代码如下

    <?php
    /**
     * Created by wenhainan
     * 网址: https://www.waytomilky.com/
     */
    namespace app\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 "结果";
    
        }
  • 相关阅读:
    关于Delphi中RS Float字段只有4位及OADateTime不能显示到秒的解决办法
    xml特殊字符处理(js)
    总结一下最近将163邮箱拖动效果改成兼容Firefox遇到的问题
    在XIB里面关于@property,@synthesize,release,dealloc的怪现象
    关于Core Data里面删除了一个被retain的NSManagedObject
    Hello cnblogs' world!
    html5离线应用程序 Amy
    css3布局相关的样式 Amy
    html5拖放API Amy
    css3MediaQueries的相关样式 Amy
  • 原文地址:https://www.cnblogs.com/wenhainan/p/16034688.html
Copyright © 2020-2023  润新知