Thinkphp5.0 cli 传参

application/command.php 代码如下 返回一个cli的控制器

 return ['app\command\test'];

application/command/test.php 代码如下 加注释

use think\console\Command;

use think\console\Input;

use think\console\Output;

class test xtends Command

{

//初始化命令 以及命令配置

protected function configure()

{

// 这里的test就是命令行think后面的参数 action 方法名 parameter 参数 (url模式)uid=42#file=ccc uid为用户名 file为做进度查询的MD5文件名

$this->setName('test')->addArgument('action')->addArgument('parameter')->setDescription('这里的test就是命令行think后面的参数 action 方法名 parameter 参数 (url模式)uid=42#file=ccc uid为用户名 file为做进度查询的MD5文件名');

}

//命令执行方法

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

{

//接收方法名 和 参数

$myparme = $input->getArguments();

$action = $myparme['action'];

$parameter = $myparme['parameter'];

//参数转义解析

$arguments = $this->ParSing($parameter);

//执行对应的方法

$info = $this->$action($arguments);

//输出返回值

$output->writeln($info);

}

//代码解析(urlget传参模式)

protected function ParSing($parameter){

//c=d#a=b#rrr=666

$parameter_array = array();

$arry = explode('#', $parameter);

foreach ($arry as $key => $value) {

$zzz = explode('=',$value);

$parameter_array[$zzz[0]] = $zzz[1];

}

return $parameter_array;

}

//测试方法

public function index($data){

echo $data['uid'];

echo $data['file'];

}

}


切换到项目目录

php think test index uid=6#file=ccc

想知道原理的

男的自己看

use think\console\Command;

use think\console\Input;

use think\console\Output;

这3个文件

你可能感兴趣的:(Thinkphp5.0 cli 传参)