最近的一个项目在用tp5,对于tp3都几乎没用过的我来讲~~~php
tp5最好的一点就是对接口的单独封装,只要严格按照要求一步一步来就能够成功了thinkphp
<?php
return [
'app\api\controller\shell\控制器名称',
];
建立命令类文件(接口文件),地址application/controller/shell/文件名称(驼峰命名)

编辑文件
<?php namespace app\home; // 此处开发文档上写的是app\home\command;实际上,去掉command才能够正常跑,尚未弄明白到底是怎么回事 use think\console\Command; use think\console\Input; use think\console\Output; 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 ');
}
shell
//获取参数值 $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..");
api
app
注意只有在配置中设置了参数,同时在execute中用封装的方法$input->getArguments();
获取参数才可以精确获取到须要的参数,不然若是只是在execute中直接print($input)出现的是一个超级复杂的数据对象
$input->getArguments();
同时能够将参数跟在命令后面当作参数传入 php think test 2017-06-05thinkphp5
这样就完成了一个简单的接口测试
可是在我刚刚进入命令行的时候,执行命令行中的tp5带命令,报了一个奇怪的错误igbinary 版本错误,缘由是我安装的igbinary与php版本不匹配,从新安装就行了,可是目前还不知道igbinary和命令行运行之间有什么必然联系this