Mix Vega
Vega 是一个用 PHP 编写的 CLI HTTP 网络框架,反对 Swoole、WorkerMan
Overview
Vega 是 MixPHP V3+
内置的最外围的组件 (可独立应用),参考
golang gin mux 开发,它蕴含 Web 利用解决的大量性能 (数据库解决除外)
,包含:路由、渲染、参数获取、中间件、文件上传解决等;具备 CLI 模式下弱小的兼容性,同时反对 Swoole、WorkerMan, 并且反对 Swoole 的多种过程模型。
源码地址
Star 一下不迷路,下次用的时候还能找到
- https://github.com/mix-php/vega
- https://gitee.com/mix-php/vega
Installation
须要先装置 Swoole 或者 WorkerMan
composer require mix/vega
Quick start
Swoole 多过程 (异步) 中应用
<?phprequire __DIR__ . '/vendor/autoload.php';$vega = new Mix\Vega\Engine();$vega->handleF('/hello', function (Mix\Vega\Context $ctx) { $ctx->string(200, 'hello, world!');})->methods('GET');$http = new Swoole\Http\Server('0.0.0.0', 9501);$http->on('Request', $vega->handler());$http->start();
Swoole 单过程 (协程) 中应用
<?phprequire __DIR__ . '/vendor/autoload.php';Swoole\Coroutine\run(function () { $vega = new Mix\Vega\Engine(); $vega->handleF('/hello', function (Mix\Vega\Context $ctx) { $ctx->string(200, 'hello, world!'); })->methods('GET'); $server = new Swoole\Coroutine\Http\Server('127.0.0.1', 9502, false); $server->handle('/', $vega->handler()); $server->start();});
WorkerMan 中应用
<?phprequire __DIR__ . '/vendor/autoload.php';$vega = new Mix\Vega\Engine();$vega->handleF('/hello', function (Mix\Vega\Context $ctx) { $ctx->string(200, 'hello, world!');})->methods('GET');$http_worker = new Workerman\Worker("http://0.0.0.0:2345");$http_worker->onMessage = $vega->handler();$http_worker->count = 4;Workerman\Worker::runAll();
拜访测试
% curl http://0.0.0.0:9501/hellohello, world!
路由配置
配置 Closure
闭包路由
$vega = new Mix\Vega\Engine();$vega->handleF('/hello', function (Mix\Vega\Context $ctx) { $ctx->string(200, 'hello, world!');})->methods('GET');
配置 callable
路由
class Hello { public function index(Mix\Vega\Context $ctx) { $ctx->string(200, 'hello, world!'); }}$vega = new Mix\Vega\Engine();$vega->handleC('/hello', [new Hello(), 'index'])->methods('GET');
配置路由变量
$vega = new Mix\Vega\Engine();$vega->handleF('/users/{id}', function (Mix\Vega\Context $ctx) { $id = $ctx->param('id'); $ctx->string(200, 'hello, world!');})->methods('GET');
配置多个 method
$vega = new Mix\Vega\Engine();$vega->handleF('hello', function (Mix\Vega\Context $ctx) { $ctx->string(200, 'hello, world!');})->methods('GET', 'POST');
路由前缀 (分组)
$vega = new Mix\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');
参数获取
申请参数
办法名称 | 形容 |
---|---|
$ctx->param(string $key): string | 获取路由参数 |
$ctx->query(string $key): string | 获取url参数,蕴含路由参数 |
$ctx->defaultQuery(string $key, string $default): string | 获取url参数,可配置默认值 |
$ctx->getQuery(string $key): string or null | 获取url参数, 可判断是否存在 |
$ctx->postForm(string $key): string | 获取post参数 |
$ctx->defaultPostForm(string $key, string $default): string | 获取post参数,可配置默认值 |
$ctx->getPostForm(string $key): string or null | 获取post参数,可判断是否存在 |
Headers, Cookies, Uri ...
办法名称 | 形容 |
---|---|
$ctx->contentType(): string | 申请类型 |
$ctx->header(string $key): string | 申请头 |
$ctx->cookie(string $name): string | cookies |
$ctx->uri(): UriInterface | 残缺uri |
$ctx->rawData(): string | 原始包数据 |
客户端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);
申请上下文
申请当中须要保留一些信息,比方:会话、JWT载荷等。
办法名称 | 形容 |
---|---|
$ctx->set(string $key, $value): void | 设置值 |
$ctx->get(string $key): mixed or null | 获取值 |
$ctx->mustGet(string $key): mixed or throws | 获取值或抛出异样 |
中断执行
abort
执行后,会进行执行前面的全副代码,包含中间件。
$vega = new Mix\Vega\Engine();$vega->handleF('/users/{id}', function (Mix\Vega\Context $ctx) { if (true) { $ctx->string(401, 'Unauthorized'); $ctx->abort(); } $ctx->string(200, 'hello, world!');})->methods('GET');
响应解决
办法名称 | 形容 |
---|---|
$ctx->status(int $code): void | 设置状态码 |
$ctx->setHeader(string $key, string $value): void | 设置header |
$ctx->setCookie(string $name, string $value, int $expire = 0, ...): void | 设置cookie |
$ctx->redirect(string $location, int $code = 302): void | 重定向 |
JSON 申请与输入
获取 JSON 申请数据
$vega = new Mix\Vega\Engine();$vega->handleF('/users', function (Mix\Vega\Context $ctx) { $obj = $ctx->getJSON(); if (!$obj) { throw new \Exception('Parameter error'); } var_dump($obj); $ctx->JSON(200, [ 'code' => 0, 'message' => 'ok' ]);})->methods('POST');
mustGetJSON
自带有效性查看,以下代码等同于下面
$vega = new Mix\Vega\Engine();$vega->handleF('/users', function (Mix\Vega\Context $ctx) { $obj = $ctx->mustGetJSON(); var_dump($obj); $ctx->JSON(200, [ 'code' => 0, 'message' => 'ok' ]);})->methods('POST');
JSONP 解决
$vega = new Mix\Vega\Engine();$vega->handleF('/jsonp', function (Mix\Vega\Context $ctx) { $ctx->JSONP(200, [ 'code' => 0, 'message' => 'ok' ]);})->methods('GET');
设置中间件
给某个路由配置中间件,可配置多个
$vega = new Mix\Vega\Engine();$func = function (Mix\Vega\Context $ctx) { // do something $ctx->next();};$vega->handleF('/hello', $func, function (Mix\Vega\Context $ctx) { $ctx->string(200, 'hello, world!');})->methods('GET');
配置全局中间件,即使没有匹配到路由也会执行
$vega = new Mix\Vega\Engine();$vega->use(function (Mix\Vega\Context $ctx) { $ctx->next();});
前置中间件
$vega->use(function (Mix\Vega\Context $ctx) { // do something $ctx->next();});
后置中间件
$vega->use(function (Mix\Vega\Context $ctx) { $ctx->next(); // do something});
404 自定义
$vega = new Mix\Vega\Engine();$vega->use(function (Mix\Vega\Context $ctx) { try{ $ctx->next(); } catch (Mix\Vega\Exception\NotFoundException $ex) { $ctx->string(404, 'New 404 response'); $ctx->abort(); }});
500 全局异样捕捉
$vega = new Mix\Vega\Engine();$vega->use(function (Mix\Vega\Context $ctx) { try{ $ctx->next(); } catch (\Throwable $ex) { $ctx->string(500, 'New 500 response'); $ctx->abort(); }});
HTML 视图渲染
创立视图文件 foo.php
<p>id: <?= $id ?>, name: <?= $name ?></p><p>friends:</p><ul> <?php foreach($friends as $name): ?> <li><?= $name ?></li> <?php endforeach; ?></ul>
配置视图门路,并响应html
$vega = new Mix\Vega\Engine();$vega->withHTMLRoot('/data/project/views');$vega->handleF('/html', function (Mix\Vega\Context $ctx) { $ctx->HTML(200, 'foo', [ 'id' => 1000, 'name' => '小明', 'friends' => [ '小花', '小红' ] ]);})->methods('GET');
License
Apache License Version 2.0, http://www.apache.org/licenses/