MixVegaVega是一个用PHP编写的CLIHTTP网络框架,支持Swoole,WorkerManOverviewVega是MixPHPV3+内置的核心组件(可独立使用),参考golangginmux开发,包含大量的web应用处理功能(数据库处理除外),包括:路由、渲染、参数获取、中间件、文件上传处理等;CLI模式下兼容性强,支持Swoole、WorkerMan,支持Swoole各种进程模型。建议配合以下数据库使用:https://github.com/mix-php/da...https://github.com/top-think/...https://github.com/illuminate...源代码地址Star一下子不会丢,下次用的时候可以找https://github.com/mix-php/vegahttps://gitee.com/mix-php/vegaTechnical交流知乎:https://www.zhihu.com/people/...官方QQ群:284806582、825122875敲代码:vega安装需要先安装Swoole或者WorkerMancomposerrequiremix/vega快速启动Swoole使用handleF('/hello',function(Mix\Vega\Context$ctx){$ctx->string(200,'你好,世界!');})->methods('GET');$http=newSwoole\Http\Server('0.0.0.0',9501);$http->on('请求',$vega->handler());$http->start();在Swoole单进程(协程)中使用handleF('/hello',function(Mix\Vega\Context$ctx){$ctx->string(200,'hello,world!');})->methods('GET');$server=newSwoole\Coroutine\Http\Server('127.0.0.1',9502,false);$server->handle('/',$vega->handler());$server->start();});WorkerMan中使用handleF('/hello',function(Mix\Vega\Context$ctx){$ctx->string(200,'你好,世界!');})->methods('GET');$http_worker=newWorkerman\Worker("http://0.0.0.0:2345");$http_worker->onMessage=$vega->handler();$http_worker->count=4;Workerman\Worker::runAll();访问测试%curlhttp://0.0.0.0:9501/hellohello,world!由配置配置关闭关闭包由$vega=newMix\Vega\Engine();$vega->handleF('/hello',function(Mix\Vega\Context$ctx){$ctx->string(200,'hello,world!');})->methods('GET');配置可调用路由classHello{publicfunctionindex(Mix\Vega\Context$ctx){$ctx->string(200,'hello,world!');}}$vega=newMix\Vega\Engine();$vega->handleC('/hello',[newHello(),'index'])->方法('获取');配置路由变量$vega=newMix\Vega\Engine();$vega->handleF('/users/{id}',function(Mix\Vega\Context$ctx){$id=$ctx->param('id');$ctx->string(200,'你好,世界!');})->methods('GET');配置多个方法$vega=newMix\Vega\Engine();$vega->handleF('hello',function(Mix\Vega\Context$ctx){$ctx->string(200,'hello,world!');})->方法('GET','POST');路由前缀(组)$vega=newMix\Vega\Engine();$subrouter=$vega->pathPrefix('/foo');$subrouter->handleF('/bar1',function(Mix\Vega\Context$ctx){$ctx->string(200,'hello,world!');})->methods('GET');$subrouter->handleF('/bar2',function(Mix\Vega\Context$ctx){$ctx->string(200,'hello1,world!');})->methods('GET');parameter获取请求参数方法名称说明$ctx->param(string$key):string获取路由参数$ctx->query(string$key):string获取url参数,包括路由参数$ctx->defaultQuery(string$key,string$default):string获取url参数,可配置默认值$ctx->getQuery(string$key):string或null获取取url参数判断是否有$ctx->postForm(string$key):string获取post参数$ctx->defaultPostForm(string$key,string$default):string获取post参数,默认value可以配置$ctx->getPostForm(string$key):stringornull获取post参数,可以判断是否有Headers,Cookies,Uri...方法名说明$ctx->contentType():string请求类型$ctx->header(string$key):stringrequestheader$ctx->cookie(string$name):stringcookies$ctx->uri():UriInterfacecompleteuri$ctx->rawData():stringrawpacketdata客户端IP方法名称说明$ctx->clientIP():string从反向代理获取用户真实IP$ctx->remoteIP():string获取远程IP上传文件处理方法名称说明$ctx->formFile(string$name):UploadedFileInterface获取第一个上传的文件$ctx->multipartForm():UploadedFileInterface[]获取所有上传的文件并保存$file=$ctx->formFile('img');$targetPath='/data/uploads/'。$file->getClientFilename();$file->moveTo($targetPath);请求上下文request中需要保存一些信息,比如:session、JWTpayload等。方法名称说明$ctx->set(string$key,$value):voidsetvalue$ctx->get(string$key):mixedornullgetvalue$ctx->mustGet(string$key):mixedorthrows获取一个值或抛出一个异常来中断执行abort执行后,将停止执行所有后续代码,包括中间件。$vega=newMix\Vega\Engine();$vega->handleF('/users/{id}',function(Mix\Vega\Context$ctx){if(true){$ctx->string(401,'未经授权');$ctx->abort();}$ctx->string(200,'hello,world!');})->methods('GET');响应处理方法名称说明$ctx->status(int$code):void设置状态码$ctx->setHeader(string$key,string$value):voidsetheader$ctx->setCookie(string$name,string$value,int$expire=0,...):voidsetcookie$ctx->redirect(string$location,int$code=302):voidredirectJSONrequestandoutput获取JSON请求数据$vega=newMix\Vega\Engine();$vega->handleF('/users',function(Mix\Vega\Context$ctx){$obj=$ctx->getJSON();if(!$obj){thrownew\Exception('参数错误');}var_dump($obj);$ctx->JSON(200,['code'=>0,'message'=>'ok']);})->methods('POST');mustGetJSON自带有效性检查,下面的代码等同于上面的$vega=newMix\Vega\Engine();$vega->handleF('/users',function(Mix\Vega\Context$ctx){$obj=$ctx->mustGetJSON();变量转储($obj);$ctx->JSON(200,['code'=>0,'message'=>'ok']);})->methods('POST');JSONP句柄$vega=newMix\Vega\Engine();$vega->handleF('/jsonp',function(Mix\Vega\Context$ctx){$ctx->JSONP(200,['code'=>0,'message'=>'ok']);})->方法('获取');setmiddleware为某个路由配置中间件,multiple$vega=newMix\Vega\Engine();$func=function(Mix\Vega\Context$ctx){//dosomething$ctx->next();};$vega->handleF('/hello',$func,function(Mix\Vega\Context$ctx){$ctx->string(200,'hello,world!');})->methods('GET');配置全局中间件,即使没有匹配到路由,也会执行$vega=newMix\Vega\Engine();$vega->use(function(Mix\Vega\Context$ctx){$ctx->next();});前置中间件$vega->use(function(Mix\Vega\Context$ctx){//做一些事情$ctx->next();});后中间件$vega->use(function(Mix\Vega\Context$ctx){$ctx->next();//做点什么});404自定义$vega=新Mix\Vega\Engine();$vega->use(function(Mix\Vega\Context$ctx){try{$ctx->next();}catch(Mix\Vega\Exception\NotFoundException$ex){$复制代码ctx->string(404,'New404response');$ctx->abort();}});500全局异常捕获$vega=newMix\Vega\Engine();$vega->use(function(Mix\Vega\Context$ctx){try{$ctx->next();}catch(\Throwable$ex){$ctx->string(500,'新的500响应');$ctx->abort();}});HTML视图渲染创建视图文件foo.php
id:=$id?>,name:=$name?>
friends:
- =$name?>