• thinkphp5 内置接口开发与使用


    最近的一个项目在用tp5,对于tp3都几乎没用过的我来说~~~

    tp5最好的一点就是对接口的单独封装,只要严格按照要求一步一步来就可以成功了

    1. 开启命令行:
      1. 配置环境变量
      2. 安装tp5项目
      3. cmd进入项目目录,运行php think,出现如下内容,则表示命令行开启成功

        具体指令参考手册 https://www.kancloud.cn/manual/thinkphp5/122951
    2. 创建自定义命令行(接口)注意命名空间问题

        1. 配置command.php文件,目录在application/command.php
          <?php

          return [
          'appapicontrollershell控制器名称',
          ];
        2. 创建命令类文件(接口文件),地址application/controller/shell/文件名称(驼峰命名)
          1. 编辑文件
            <?php namespace apphome;    // 此处开发文档上写的是apphomecommand;实际上,去掉command才可以正常跑,还没有弄明白究竟是怎么回事 use thinkconsoleCommand; use thinkconsoleInput; use thinkconsoleOutput; class Test extends Command {     protected function configure()     {        
                  //设置参数
                    $this->addArgument('email', Argument::REQUIRED); //必传参数
                    $this->addArgument('mobile', Argument::OPTIONAL);//可选参数
            
            
                    //选项定义
                    $this->addOption('message', 'm', Option::VALUE_REQUIRED, 'test'); //选项值必填
                    $this->addOption('status', 's', Option::VALUE_OPTIONAL, 'test'); //选项值选填
            
                    $this->setName('test')->setDescription('Here is the remark ');
                }     

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

                    //获取参数值
                        $args = $input->getArguments();
                        $output->writeln('The args value is:');
                        print_r($args);
                        //获取选项值
                        $options = $input->getOptions();
                        $output->writeln('The options value is:');
                        print_r($options);
        
                        $output->writeln('Now execute command...');
        
                        $output->writeln("End..");

                  }

                }

        注意只有在配置中设置了参数,同时在execute中用封装的方法$input->getArguments();获取参数才能够精确获取到需要的参数,否则如果只是在execute中直接print($input)出现的是一个超级复杂的数据对象
    3.  

      测试-命令帮助-命令行下运行  php think

    4. 运行test命令  php think test

      同时可以将参数跟在命令后面当做参数传入  php think test 2017-06-05

      这样就完成了一个简单的接口

      但是在我刚刚进入命令行的时候,执行命令行中的tp5带命令,报了一个奇怪的错误igbinary 版本错误,原因是我安装的igbinary与php版本不匹配,重新安装就好了,但是目前还不知道igbinary和命令行运行之间有什么必然联系

  • 相关阅读:
    [原创]K8Cscan插件之Mysql密码爆破(内网渗透/支持批量/可跨网段)
    [原创]K8Cscan插件之FTP弱口令扫描(内网渗透/支持批量/可跨网段)
    [原创]K8Cscan插件之Web主机扫描(存活主机、机器名、Banner、标题)(内网渗透/支持批量/可跨网段)
    [原创]K8Cscan插件之存活主机扫描(内网渗透/支持批量/可跨网段)
    [原创]K8 Jboss jmx-console getshell exploit
    [原创]K8Cscan插件之C段旁站扫描子域名扫描
    [原创]K8mysqlCmd数据库免驱连接工具
    [原创]k8exe2bat任意文件转Bat工具(WebShell无法上传EXE解决方案)
    Tensorflow 笔记:第一讲
    数据结构的C语言基础
  • 原文地址:https://www.cnblogs.com/wangfengzhu/p/6946376.html
Copyright © 2020-2023  润新知