多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
1,以为下方图片为例,在api模块下创建了一个command目录,然后创建自己所需要的命令行执行文件 我的执行文件未Online.php ![](https://img.kancloud.cn/9b/c0/9bc0bc4cdd287c03a7eed01922337e03_474x117.png) 以下为代码实例 ```php <?php namespace app\api\command; use think\console\Input; use think\console\input\Argument; use think\console\Output; use think\console\Command; /** * @package app\api\command */ class Online extends Command { /** * 配置指令 */ protected function configure() { $this->addArgument('task_id', Argument::OPTIONAL); $this->addArgument('sex', Argument::OPTIONAL); //此处为获取执行命令的参数,并且定义参数key $this->setName('online:member')->setDescription('online member'); } protected function execute(Input $input, Output $output) { $args = $input->getArguments(); $task_id=$args['task_id']; $sex=$args['sex']; } } ``` > 代码解读 > 此处需要继承thinkphp的command类,才能让你的执行文件加载 > configure 为配置方法 > $this->addArgument('task_id', Argument::OPTIONAL);为获取命令中的参数 > 比如 php think online:member 1 2 > 那么第一个 task_id=1 > 第二个 task_id=2 > execute 执行方法 > $args = $input->getArguments(); 获取定义的参数 然后在application/你的模块目录下创建command.php 这些搞定后,找到thinkphp config目录 ![](https://img.kancloud.cn/be/f6/bef610036f3a4e4f0a6b06b9d7231519_481x319.png) 直接 return你的命名空间即可使用了 ```php <?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: yunwuxin <448901948@qq.com> // +---------------------------------------------------------------------- return ['app\api\command\Online']; ``` 那么就让我们来一起试一试吧