首先在application 文件夹下建立一个新文件夹command(用来放我们自定义的Tp命令) 我们取名叫Hello.php(随意取)
setName('hello')
->setDescription('hello word !');
}
/**
* 重写execute
* {@inheritdoc}
*/
protected function execute(Input $input, Output $output)
{
$output->writeln('hello word !');
}
}
?>
编辑 application/command.php(没有就新建) 加上Hello.php文件的namespace
setName('hello') //命令名称
->setDefinition([ //Option 和 Argument都可以有多个, Argument的读取和定义的顺序有关.请注意
new Option('option', 'o', Option::VALUE_OPTIONAL, "命令option选项"), //使用方式 php think hello --option test 或 -o test
new Argument('test',Argument::OPTIONAL,"test参数"), //使用方式 php think hello test1 (将输入的第一个参数test1赋值给定义的第一个Argument参数test)
//...
])
->setDescription('hello word !'); //命令描述
}
/**
* 重写execute ---执行入口
* {@inheritdoc}
*/
protected function execute(Input $input, Output $output)
{ //Input 用于获取输入信息 Output用于输出信息
$request = Request::instance([ //如果在希望代码中像浏览器一样使用input()等函数你需要示例化一个Request并手动赋值
'get'=>$input->getArguments(), //示例1: 将input->Arguments赋值给Request->get 在代码中可以直接使用input('get.')获取参数
'route'=>$input->getOptions() //示例2: 将input->Options赋值给Request->route 在代码中可以直接使用request()->route(false)获取
//...
]);
$request->module("Index"); //绑定当前模块为Index 只有绑定模块你在代码中使用model('user')等函数才不需要指定模块model('index/user')
// $request->controller('index')->action('index'); //绑定controller和action
$output->writeln(controller('index/Index')->index()); //执行index模块index控制器index函数 ,并输出返回值
}
}