资料介绍
软件简介
Vega 是一个用 PHP 编写的 CLI 模式 HTTP 网络框架,支持 Swoole、WorkerMan、FPM、CLI-Server
概述
Vega 是?MixPHP?V3+
?内置的最核心的组件 (可独立使用),参考 golang?gin?mux?开发,它包含 Web 应用处理的大量功能 (数据库处理除外) ,包括:路由、渲染、参数获取、中间件、文件上传、静态文件处理等;具有 CLI 模式下强大的兼容性,同时支持 Swoole、WorkerMan、FPM、CLI-Server, 并且支持 Swoole 的多种进程模型与协程。
推荐搭配以下数据库使用:
- https://github.com/mix-php/database
- https://github.com/mix-php/redis
- https://github.com/top-think/think-orm
- https://github.com/illuminate/database
推荐文章:
技术交流
知乎:https://www.zhihu.com/people/onanying
官方QQ群:284806582?,?825122875?敲门暗号:vega
安装
composer require mix/vega
快速开始
- Swoole 多进程 (异步) 中使用
require __DIR__ . '/vendor/autoload.php'; $vega = new Mix\Vega\Engine(); $vega->handle('/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->set([ 'worker_num' => 4, ]); $http->start();
开启多进程协程
$http->on('Request', $vega->handler()); $http->on('WorkerStart', function ($server, $workerId) { // 协程初始化 // 比如:启动 mix/database mix/redis 的连接池 }); $http->set([ 'enable_coroutine' => true, 'worker_num' => 4, ]);
php swoole.php
- Swoole 单进程 (协程) 中使用
require __DIR__ . '/vendor/autoload.php'; Swoole\Coroutine\run(function () { $vega = new Mix\Vega\Engine(); $vega->handle('/hello', function (Mix\Vega\Context $ctx) { $ctx->string(200, 'hello, world!'); })->methods('GET'); $server = new Swoole\Coroutine\Http\Server('0.0.0.0', 9502, false); $server->handle('/', $vega->handler()); $server->start(); });
php swooleco.php
- WorkerMan 中使用
require __DIR__ . '/vendor/autoload.php'; $vega = new Mix\Vega\Engine(); $vega->handle('/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();
php wokerman.php start
- PHP-FPM 中使用
在?nginx
?配置?rewrite
?重写到?index.php
require __DIR__ . '/vendor/autoload.php'; $vega = new Mix\Vega\Engine(); $vega->handle('/hello', function (Mix\Vega\Context $ctx) { $ctx->string(200, 'hello, world!'); })->methods('GET'); return $vega->run();
- PHP?cli-server?中使用
这个内置的Web服务器主要用于本地开发使用,不可用于线上产品环境。
require __DIR__ . '/vendor/autoload.php'; $vega = new Mix\Vega\Engine(); $vega->handle('/hello', function (Mix\Vega\Context $ctx) { $ctx->string(200, 'hello, world!'); })->methods('GET'); return $vega->run();
php -S localhost:8000 router.php
- 访问测试
% curl http://127.0.0.1:9501/hello hello, world!
路由配置
配置?Closure
?闭包路由
$vega = new Mix\Vega\Engine(); $vega->handle('/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->handle('/hello', [new Hello(), 'index'])->methods('GET');
配置路由变量
$vega = new Mix\Vega\Engine(); $vega->handle('/users/{id:\d+}', function (Mix\Vega\Context $ctx) { $id = $ctx->param('id'); $ctx->string(200, 'hello, world!'); })->methods('GET');
配置多个?method
$vega = new Mix\Vega\Engine(); $vega->handle('/hello', function (Mix\Vega\Context $ctx) { $ctx->string(200, 'hello, world!'); })->methods('GET', 'POST');
路由前缀 (分组)
$vega = new Mix\Vega\Engine(); $sub = $vega->pathPrefix('/foo'); $sub->handle('/bar1', function (Mix\Vega\Context $ctx) { $ctx->string(200, 'hello, world!'); })->methods('GET'); $sub->handle('/bar2', function (Mix\Vega\Context $ctx) { $ctx->string(200, 'hello1, world!'); })->methods('GET');
参数获取
请求参数
方法名称 | 描述 |
---|---|
$ctx->request: ServerRequestInterface | 符合PSR的请求对象 |
$ctx->response: ResponseInterface | 符合PSR的响应对象 |
ctx?>param(stringctx?>param(stringkey): string | 获取路由参数 |
ctx?>query(stringctx?>query(stringkey): string | 获取url参数,包含路由参数 |
ctx?>defaultQuery(stringctx?>defaultQuery(stringkey, string $default): string | 获取url参数,可配置默认值 |
ctx?>getQuery(stringctx?>getQuery(stringkey): string or null | 获取url参数, 可判断是否存在 |
ctx?>postForm(stringctx?>postForm(stringkey): string | 获取post参数 |
ctx?>defaultPostForm(stringctx?>defaultPostForm(stringkey, string $default): string | 获取post参数,可配置默认值 |
ctx?>getPostForm(stringctx?>getPostForm(stringkey): string or null | 获取post参数,可判断是否存在 |
Headers, Cookies, Uri ...
方法名称 | 描述 |
---|---|
$ctx->contentType(): string | 请求类型 |
ctx?>header(stringctx?>header(stringkey): string | 请求头 |
ctx?>cookie(stringctx?>cookie(stringname): string | cookies |
$ctx->uri(): UriInterface | 完整uri |
$ctx->rawData(): string | 原始包数据 |
客户端IP
方法名称 | 描述 |
---|---|
$ctx->clientIP(): string | 从反向代理获取用户真实IP |
$ctx->remoteIP(): string | 获取远程IP |
上传文件处理
方法名称 | 描述 |
---|---|
ctx?>formFile(stringctx?>formFile(stringname): UploadedFileInterface | 获取上传的第一个文件 |
$ctx->multipartForm(): UploadedFileInterface[] | 获取上传的全部文件 |
文件保存
$file = $ctx->formFile('img'); $targetPath = '/data/project/public/uploads/' . $file->getClientFilename(); $file->moveTo($targetPath);
请求上下文
请求当中需要保存一些信息,比如:会话、JWT载荷等。
方法名称 | 描述 |
---|---|
ctx?>set(stringctx?>set(stringkey, $value): void | 设置值 |
ctx?>get(stringctx?>get(stringkey): mixed or null | 获取值 |
ctx?>mustGet(stringctx?>mustGet(stringkey): mixed or throws | 获取值或抛出异常 |
中断执行
abort
?执行后,会停止执行后面的全部代码,包括中间件。
方法名称 | 描述 |
---|---|
$ctx->abort(): void | 中断,需自行处理响应 |
ctx?>abortWithStatus(intctx?>abortWithStatus(intcode): void | 中断并响应状态码 |
ctx?>abortWithStatusJSON(intctx?>abortWithStatusJSON(intcode, $data): void | 中断并响应JSON |
$vega = new Mix\Vega\Engine(); $vega->handle('/users/{id}', function (Mix\Vega\Context $ctx) { if (true) { $ctx->string(401, 'Unauthorized'); $ctx->abort(); } $ctx->string(200, 'hello, world!'); })->methods('GET');
响应处理
方法名称 | 描述 |
---|---|
ctx?>status(intctx?>status(intcode): void | 设置状态码 |
ctx?>setHeader(stringctx?>setHeader(stringkey, string $value): void | 设置header |
ctx?>setCookie(stringctx?>setCookie(stringname, string?value,intvalue,intexpire = 0, ...): void | 设置cookie |
ctx?>redirect(stringctx?>redirect(stringlocation, int $code = 302): void | 重定向 |
JSON 请求与输出
获取 JSON 请求数据
$vega = new Mix\Vega\Engine(); $vega->handle('/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->handle('/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->handle('/jsonp', function (Mix\Vega\Context $ctx) { $ctx->JSONP(200, [ 'code' => 0, 'message' => 'ok' ]); })->methods('GET');
HTML 视图渲染
创建视图文件?foo.php
id: = $id ?>, name: = $name ?> friends:foreach($friends as $name): ?> = $name ?> endforeach; ?>
配置视图路径,并响应html
$vega = new Mix\Vega\Engine(); $vega->withHTMLRoot('/data/project/views'); $vega->handle('/html', function (Mix\Vega\Context $ctx) { $ctx->HTML(200, 'foo', [ 'id' => 1000, 'name' => '小明', 'friends' => [ '小花', '小红' ] ]); })->methods('GET');
静态文件处理
基于?sendfile
?零拷贝,不支持在?PHP-FPM
?中使用
$vega = new Mix\Vega\Engine(); $vega->static('/static', '/data/project/public/static'); $vega->staticFile('/favicon.ico', '/data/project/public/favicon.ico');
设置中间件
给某个路由配置中间件,可配置多个
$vega = new Mix\Vega\Engine(); $func = function (Mix\Vega\Context $ctx) { // do something $ctx->next(); }; $vega->handle('/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) { if ($ex instanceof Mix\Vega\Abort || $ex instanceof Mix\Vega\Exception\NotFoundException) { throw $ex; } $ctx->string(500, 'New 500 response'); $ctx->abort(); } });
License
Apache License Version 2.0,?http://www.apache.org/licenses/
- ChatGPT:AI模型框架研究 9次下载
- MA5680T设备CLI的基本操作 4次下载
- ani-cli浏览和观看动漫的cli
- OpenHarmony上使用的Http网络框架教程 4次下载
- 智能Mesh IP V经理CLI指南
- 基于情感评分的分层注意力网络框架 5次下载
- 智能Mesh Web HART经理CLI指南
- SmartMesh IP Embedded Manager CLI指南
- SmartMesh IP Mote CLI指南
- SmartMesh WirelessHart Mote CLI指南
- 基于时间卷积网络的通用日志序列异常检测框架 8次下载
- Navicat for MySQL如何才能设置HTTP属性 6次下载
- 网络资源分配框架的软件设计和应用 10次下载
- SmartMesh WirelessHART经理CLI指南 0次下载
- 基于攻击事件的动态网络风险评估框架 0次下载
- 高性能网络框架之XDP技术解析 3916次阅读
- 如何使用 ESP-AT实现HTTP请求 1294次阅读
- 简化网络自动化任务编排框架Nornir的用法 2268次阅读
- 大话HTTP协议前世今生 723次阅读
- Http是什么 5276次阅读
- STM32G0开发笔记:FreeRTOS和CLI组件使用 3741次阅读
- JustWeEngine游戏框架网络事件处理 Http服务器的数据交换 1731次阅读
- 详解Netty高性能异步事件驱动的网络框架 1871次阅读
- 鸿蒙环境下的Http网络访问完成设计 1778次阅读
- Linux操作系统解读:GUI、CLI 和 TUI 3996次阅读
- 如何发起 HTTP 请求流程 4975次阅读
- 基于TensorFlow框架搭建卷积神经网络的电池片缺陷识别研究 7802次阅读
- HTTP协议的使用方式和设计原理讲解 4272次阅读
- 基于Simplicity Studio的 CLI便捷地调试Zigbee网络 9331次阅读
- OMCS网络语音视频聊天框架功能及技术分析 2633次阅读
下载排行
本周
- 1山景DSP芯片AP8248A2数据手册
- 1.06 MB | 532次下载 | 免费
- 2RK3399完整板原理图(支持平板,盒子VR)
- 3.28 MB | 339次下载 | 免费
- 3TC358743XBG评估板参考手册
- 1.36 MB | 330次下载 | 免费
- 4DFM软件使用教程
- 0.84 MB | 295次下载 | 免费
- 5元宇宙深度解析—未来的未来-风口还是泡沫
- 6.40 MB | 227次下载 | 免费
- 6迪文DGUS开发指南
- 31.67 MB | 194次下载 | 免费
- 7元宇宙底层硬件系列报告
- 13.42 MB | 182次下载 | 免费
- 8FP5207XR-G1中文应用手册
- 1.09 MB | 178次下载 | 免费
本月
- 1OrCAD10.5下载OrCAD10.5中文版软件
- 0.00 MB | 234315次下载 | 免费
- 2555集成电路应用800例(新编版)
- 0.00 MB | 33566次下载 | 免费
- 3接口电路图大全
- 未知 | 30323次下载 | 免费
- 4开关电源设计实例指南
- 未知 | 21549次下载 | 免费
- 5电气工程师手册免费下载(新编第二版pdf电子书)
- 0.00 MB | 15349次下载 | 免费
- 6数字电路基础pdf(下载)
- 未知 | 13750次下载 | 免费
- 7电子制作实例集锦 下载
- 未知 | 8113次下载 | 免费
- 8《LED驱动电路设计》 温德尔著
- 0.00 MB | 6656次下载 | 免费
总榜
- 1matlab软件下载入口
- 未知 | 935054次下载 | 免费
- 2protel99se软件下载(可英文版转中文版)
- 78.1 MB | 537798次下载 | 免费
- 3MATLAB 7.1 下载 (含软件介绍)
- 未知 | 420027次下载 | 免费
- 4OrCAD10.5下载OrCAD10.5中文版软件
- 0.00 MB | 234315次下载 | 免费
- 5Altium DXP2002下载入口
- 未知 | 233046次下载 | 免费
- 6电路仿真软件multisim 10.0免费下载
- 340992 | 191187次下载 | 免费
- 7十天学会AVR单片机与C语言视频教程 下载
- 158M | 183279次下载 | 免费
- 8proe5.0野火版下载(中文版免费下载)
- 未知 | 138040次下载 | 免费
评论