MixVega CLI HTTP 网络框架开源项目

我要开发同款
匿名用户2021年06月29日
101阅读

技术信息

开源地址
https://github.com/mix-php/vega
授权协议
Apache

作品详情

Vega是一个用PHP编写的CLI模式HTTP网络框架,支持Swoole、WorkerMa、FPM、CLI-Server

概述Vega是 MixPHP V3+ 内置的最核心的组件(可独立使用),参考golag gi mux 开发,它包含Web应用处理的大量功能(数据库处理除外),包括:路由、渲染、参数获取、中间件、文件上传、静态文件处理等;具有CLI模式下强大的兼容性,同时支持Swoole、WorkerMa、FPM、CLI-Server,并且支持Swoole的多种进程模型与协程。

推荐搭配以下数据库使用:

https://github.com/mix-php/databasehttps://github.com/mix-php/redishttps://github.com/top-thik/thik-ormhttps://github.com/illumiate/database推荐文章:

使用mix/vega+mix/db进行现代化的原生PHP开发技术交流知乎:https://www.zhihu.com/people/oayig官方QQ群:284806582 , 825122875 敲门暗号:vega

安装

需先安装 Swoole 或者 WorkerMa

composerrequiremix/vega快速开始Swoole多进程(异步)中使用<?phprequire__DIR__.'/vedor/autoload.php';$vega=ewMix\Vega\Egie();$vega->hadle('/hello',fuctio(Mix\Vega\Cotext$ctx){$ctx->strig(200,'hello,world!');})->methods('GET');$http=ewSwoole\Http\Server('0.0.0.0',9501);$http->o('Request',$vega->hadler());$http->set(['worker_um'=>4,]);$http->start();开启多进程协程

$http->o('Request',$vega->hadler());$http->o('WorkerStart',fuctio($server,$workerId){//协程初始化//比如:启动mix/databasemix/redis的连接池});$http->set(['eable_coroutie'=>true,'worker_um'=>4,]);phpswoole.phpSwoole单进程(协程)中使用<?phprequire__DIR__.'/vedor/autoload.php';Swoole\Coroutie\ru(fuctio(){$vega=ewMix\Vega\Egie();$vega->hadle('/hello',fuctio(Mix\Vega\Cotext$ctx){$ctx->strig(200,'hello,world!');})->methods('GET');$server=ewSwoole\Coroutie\Http\Server('0.0.0.0',9502,false);$server->hadle('/',$vega->hadler());$server->start();});phpswooleco.phpWorkerMa中使用<?phprequire__DIR__.'/vedor/autoload.php';$vega=ewMix\Vega\Egie();$vega->hadle('/hello',fuctio(Mix\Vega\Cotext$ctx){$ctx->strig(200,'hello,world!');})->methods('GET');$http_worker=ewWorkerma\Worker("https://0.0.0.0:2345");$http_worker->oMessage=$vega->hadler();$http_worker->cout=4;Workerma\Worker::ruAll();phpwokerma.phpstartPHP-FPM中使用在 gix 配置 rewrite 重写到 idex.php

<?phprequire__DIR__.'/vedor/autoload.php';$vega=ewMix\Vega\Egie();$vega->hadle('/hello',fuctio(Mix\Vega\Cotext$ctx){$ctx->strig(200,'hello,world!');})->methods('GET');retur$vega->ru();PHP cli-server 中使用这个内置的Web服务器主要用于本地开发使用,不可用于线上产品环境。

<?phprequire__DIR__.'/vedor/autoload.php';$vega=ewMix\Vega\Egie();$vega->hadle('/hello',fuctio(Mix\Vega\Cotext$ctx){$ctx->strig(200,'hello,world!');})->methods('GET');retur$vega->ru();php-Slocalhost:8000router.php访问测试%curlhttps://127.0.0.1:9501/hellohello,world!路由配置配置 Closure 闭包路由

$vega=ewMix\Vega\Egie();$vega->hadle('/hello',fuctio(Mix\Vega\Cotext$ctx){$ctx->strig(200,'hello,world!');})->methods('GET');配置 callable 路由

classHello{publicfuctioidex(Mix\Vega\Cotext$ctx){$ctx->strig(200,'hello,world!');}}$vega=ewMix\Vega\Egie();$vega->hadle('/hello',[ewHello(),'idex'])->methods('GET');配置路由变量

$vega=ewMix\Vega\Egie();$vega->hadle('/users/{id:\d+}',fuctio(Mix\Vega\Cotext$ctx){$id=$ctx->param('id');$ctx->strig(200,'hello,world!');})->methods('GET');配置多个 method

$vega=ewMix\Vega\Egie();$vega->hadle('/hello',fuctio(Mix\Vega\Cotext$ctx){$ctx->strig(200,'hello,world!');})->methods('GET','POST');路由前缀(分组)$vega=ewMix\Vega\Egie();$sub=$vega->pathPrefix('/foo');$sub->hadle('/bar1',fuctio(Mix\Vega\Cotext$ctx){$ctx->strig(200,'hello,world!');})->methods('GET');$sub->hadle('/bar2',fuctio(Mix\Vega\Cotext$ctx){$ctx->strig(200,'hello1,world!');})->methods('GET');参数获取请求参数方法名称描述$ctx->request:ServerRequestIterface符合PSR的请求对象$ctx->respose:ResposeIterface符合PSR的响应对象ctx−>param(strigctx−>param(strigkey):strig获取路由参数ctx−>query(strigctx−>query(strigkey):strig获取url参数,包含路由参数ctx−>defaultQuery(strigctx−>defaultQuery(strigkey,strig$default):strig获取url参数,可配置默认值ctx−>getQuery(strigctx−>getQuery(strigkey):strigorull获取url参数,可判断是否存在ctx−>postForm(strigctx−>postForm(strigkey):strig获取post参数ctx−>defaultPostForm(strigctx−>defaultPostForm(strigkey,strig$default):strig获取post参数,可配置默认值ctx−>getPostForm(strigctx−>getPostForm(strigkey):strigorull获取post参数,可判断是否存在Headers,Cookies,Uri...方法名称描述$ctx->cotetType():strig请求类型ctx−>header(strigctx−>header(strigkey):strig请求头ctx−>cookie(strigctx−>cookie(strigame):strigcookies$ctx->uri():UriIterface完整uri$ctx->rawData():strig原始包数据客户端IP方法名称描述$ctx->clietIP():strig从反向代理获取用户真实IP$ctx->remoteIP():strig获取远程IP上传文件处理方法名称描述ctx−>formFile(strigctx−>formFile(strigame):UploadedFileIterface获取上传的第一个文件$ctx->multipartForm():UploadedFileIterface[]获取上传的全部文件文件保存

$file=$ctx->formFile('img');$targetPath='/data/project/public/uploads/'.$file->getClietFileame();$file->moveTo($targetPath);请求上下文请求当中需要保存一些信息,比如:会话、JWT载荷等。

方法名称描述ctx−>set(strigctx−>set(strigkey,$value):void设置值ctx−>get(strigctx−>get(strigkey):mixedorull获取值ctx−>mustGet(strigctx−>mustGet(strigkey):mixedorthrows获取值或抛出异常中断执行abort 执行后,会停止执行后面的全部代码,包括中间件。

方法名称描述$ctx->abort():void中断,需自行处理响应ctx−>abortWithStatus(itctx−>abortWithStatus(itcode):void中断并响应状态码ctx−>abortWithStatusJSON(itctx−>abortWithStatusJSON(itcode,$data):void中断并响应JSON$vega=ewMix\Vega\Egie();$vega->hadle('/users/{id}',fuctio(Mix\Vega\Cotext$ctx){if(true){$ctx->strig(401,'Uauthorized');$ctx->abort();}$ctx->strig(200,'hello,world!');})->methods('GET');响应处理方法名称描述ctx−>status(itctx−>status(itcode):void设置状态码ctx−>setHeader(strigctx−>setHeader(strigkey,strig$value):void设置headerctx−>setCookie(strigctx−>setCookie(strigame,strig value,itvalue,itexpire=0,...):void设置cookiectx−>redirect(strigctx−>redirect(striglocatio,it$code=302):void重定向JSON请求与输出获取JSON请求数据

$vega=ewMix\Vega\Egie();$vega->hadle('/users',fuctio(Mix\Vega\Cotext$ctx){$obj=$ctx->getJSON();if(!$obj){throwew\Exceptio('Parametererror');}var_dump($obj);$ctx->JSON(200,['code'=>0,'message'=>'ok']);})->methods('POST');mustGetJSON 自带有效性检查,以下代码等同于上面

$vega=ewMix\Vega\Egie();$vega->hadle('/users',fuctio(Mix\Vega\Cotext$ctx){$obj=$ctx->mustGetJSON();var_dump($obj);$ctx->JSON(200,['code'=>0,'message'=>'ok']);})->methods('POST');JSONP处理$vega=ewMix\Vega\Egie();$vega->hadle('/jsop',fuctio(Mix\Vega\Cotext$ctx){$ctx->JSONP(200,['code'=>0,'message'=>'ok']);})->methods('GET');HTML视图渲染创建视图文件 foo.php

<p>id:<?=$id?>,ame:<?=$ame?></p><p>frieds:</p><ul><?phpforeach($friedsas$ame):?><li><?=$ame?></li><?phpedforeach;?></ul>配置视图路径,并响应html

$vega=ewMix\Vega\Egie();$vega->withHTMLRoot('/data/project/views');$vega->hadle('/html',fuctio(Mix\Vega\Cotext$ctx){$ctx->HTML(200,'foo',['id'=>1000,'ame'=>'小明','frieds'=>['小花','小红']]);})->methods('GET');静态文件处理基于 sedfile 零拷贝,不支持在 PHP-FPM 中使用

$vega=ewMix\Vega\Egie();$vega->static('/static','/data/project/public/static');$vega->staticFile('/favico.ico','/data/project/public/favico.ico');设置中间件给某个路由配置中间件,可配置多个

$vega=ewMix\Vega\Egie();$fuc=fuctio(Mix\Vega\Cotext$ctx){//dosomethig$ctx->ext();};$vega->hadle('/hello',$fuc,fuctio(Mix\Vega\Cotext$ctx){$ctx->strig(200,'hello,world!');})->methods('GET');配置全局中间件,即便没有匹配到路由也会执行

$vega=ewMix\Vega\Egie();$vega->use(fuctio(Mix\Vega\Cotext$ctx){$ctx->ext();});前置中间件

$vega->use(fuctio(Mix\Vega\Cotext$ctx){//dosomethig$ctx->ext();});后置中间件

$vega->use(fuctio(Mix\Vega\Cotext$ctx){$ctx->ext();//dosomethig});404自定义$vega=ewMix\Vega\Egie();$vega->use(fuctio(Mix\Vega\Cotext$ctx){try{$ctx->ext();}catch(Mix\Vega\Exceptio\NotFoudExceptio$ex){$ctx->strig(404,'New404respose');$ctx->abort();}});500全局异常捕获$vega=ewMix\Vega\Egie();$vega->use(fuctio(Mix\Vega\Cotext$ctx){try{$ctx->ext();}catch(\Throwable$ex){if($existaceofMix\Vega\Abort||$existaceofMix\Vega\Exceptio\NotFoudExceptio){throw$ex;}$ctx->strig(500,'New500respose');$ctx->abort();}});LiceseApacheLiceseVersio2.0, https://www.apache.org/liceses/

功能介绍

Vega 是一个用 PHP 编写的 CLI 模式 HTTP 网络框架,支持 Swoole、WorkerMan、FPM、CLI-Server 概述 Vega 是 MixPHP V3+ 内置的最核心...

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论