ThinkPHP5.0框架自定义命令行

Swoole是PHP的异步、并行、高性能网络通信引擎,使用纯C语言编写,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,异步Redis,数据库连接池,AsyncTask,消息队列,毫秒定时器,异步文件读写,异步DNS查询。 Swoole内置了Http/WebSocket服务器端/客户端、Http2.0服务器端/客户端。

上面只是官方概念!

我在项目中主要使用Swoole拓展实现消息队列,挺高任务的执行效率。

框架环境:ThinkPHP 5.0
第一部分:ThinkPHP5.0框架自定义命令行配置
  1. 配置command.php文件,目录在application/command.php。
return [
    'app\console\command\Test',
];
复制代码
  1. 建立命令类文件,新建application/console/command/Test.php。

复制代码

command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Test extends Command{

    /**
     * 定义命令
     * 命令名称是 test
     */
    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 ');
    }


    /**
     * 命令执行的内容
     * @param Input $input
     * @param Output $output
     */
    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("TestCommand:");
        $output->writeln("End..");
    }
}
复制代码
  1. 在框架的目录下面运行命令
php think test email mobile  -m"mtset" -s"stest"
复制代码

至此,自定义命令行已经完成!

你可能感兴趣的:(ThinkPHP5.0框架自定义命令行)