您的位置:首页 > 编程语言 > PHP开发

ThinkPHP5.0框架自定义命令行

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

上面只是官方概念!

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

框架环境:ThinkPHP 5.0
第一部分:ThinkPHP5.0框架自定义命令行配置

配置command.php文件,目录在application/command.php。

return [
'app\console\command\Test',
];


建立命令类文件,新建application/console/command/Test.php。

复制代码

<?php

/**
* Created by PhpStorm.
* User: 小良
* Date: 2017/7/24
* Time: 10:55
*/

namespace app\console\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..");
}
}


在框架的目录下面运行命令

php think test email mobile  -m"mtset" -s"stest"




至此,自定义命令行已经完成!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PHP ThinkPHP Swoole Optional