Mix Vega 发布,支持 Swoole、WorkerMan 的 CLI HTTP 网络框架
ccwgpt 2024-10-10 04:55 30 浏览 0 评论
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 多进程 (异步) 中使用
<?php
require __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 单进程 (协程) 中使用
<?php
require __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 中使用
<?php
require __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/hello
hello, 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/
相关推荐
- 滨州维修服务部“一区一策”强服务
-
今年以来,胜利油田地面工程维修中心滨州维修服务部探索实施“一区一策”服务模式,持续拓展新技术应用场景,以优质的服务、先进的技术,助力解决管理区各类维修难题。服务部坚持问题导向,常态化对服务范围内的13...
- 谷歌A2A协议和MCP协议有什么区别?A2A和MCP的差异是什么?
-
在人工智能的快速发展中,如何实现AI模型与外部系统的高效协作成为关键问题。谷歌主导的A2A协议(Agent-to-AgentProtocol)和Anthropic公司提出的MCP协议(ModelC...
- 谷歌大脑用架构搜索发现更好的特征金字塔结构,超越Mask-RCNN等
-
【新智元导读】谷歌大脑的研究人员发表最新成果,他们采用神经结构搜索发现了一种新的特征金字塔结构NAS-FPN,可实现比MaskR-CNN、FPN、SSD更快更好的目标检测。目前用于目标检测的最先...
- 一文彻底搞懂谷歌的Agent2Agent(A2A)协议
-
前段时间,相信大家都被谷歌发布的Agent2Agent开源协议刷屏了,简称A2A。谷歌官方也表示,A2A是在MCP之后的补充,也就是MCP可以强化大模型/Agent的能力,但每个大模型/Agent互为...
- 谷歌提出创新神经记忆架构,突破Transformer长上下文限制
-
让AI模型拥有人类的记忆能力一直是学界关注的重要课题。传统的深度学习模型虽然在许多任务上取得了显著成效,但在处理需要长期记忆的任务时往往力不从心。就像人类可以轻松记住数天前看过的文章重点,但目前的...
- 不懂设计?AI助力,人人都能成为UI设计师!
-
最近公司UI资源十分紧张,急需要通过AI来解决UI人员不足问题,我在网上发现了几款AI应用非常适合用来进行UI设计。以下是一些目前非常流行且功能强大的工具,它们能够提高UI设计效率,并帮助设计师创造出...
- 速来!手把手教你用AI完成UI界面设计
-
晨星技术说晨星技术小课堂第二季谭同学-联想晨星用户体验设计师-【晨星小课堂】讲师通过简单、清晰的语言描述就能够用几十秒自动生成一组可编辑的UI界面,AIGC对于UI设计师而言已经逐步发展成了帮助我们...
- 「分享」一端录制,多端使用的便捷 UI 自动化测试工具,开源
-
一、项目介绍Recorder是一款UI录制和回归测试工具,用于录制浏览器页面UI的操作。通过UIRecorder的录制功能,可以在自测的同时,完成测试过程的录制,生成JavaScr...
- APP自动化测试系列之Appium介绍及运行原理
-
在面试APP自动化时,有的面试官可能会问Appium的运行原理,以下介绍Appium运行原理。Appium介绍Appium概念Appium是一个开源测试自动化框架,可用于原生,混合和移动Web应用程序...
- 【推荐】一个基于 SpringBoot 框架开发的 OA 办公自动化系统
-
如果您对源码&技术感兴趣,请点赞+收藏+转发+关注,大家的支持是我分享最大的动力!!!项目介绍oasys是一个基于springboot框架开发的OA办公自动化系统,旨在提高组织的日常运作和管理...
- 自动化实践之:从UI到接口,Playwright给你全包了!
-
作者:京东保险宋阳1背景在车险系统中,对接保司的数量众多。每当系统有新功能迭代后,基本上各个保司的报价流程都需要进行回归测试。由于保司数量多,回归测试的场景也会变得重复而繁琐,给测试团队带来了巨大的...
- 销帮帮CRM移动端UI自动化测试实践:Playwright的落地与应用
-
实施背景销帮帮自2015年成立以来,移动端UI自动化测试的落地举步维艰,移动端的UI自动化测试一直以来都未取得良好的落地。然而移动互联网时代,怎样落地移动端的UI自动化测试以快速稳定进行移动端的端到端...
- 编写自动化框架不知道该如何记录日志吗?3个方法打包呈现给你。
-
目录结构1.loguru介绍1.1什么是日志?程序运行过程中,难免会遇到各种报错。如果这种报错是在本地发现的,你还可以进行debug。但是如果程序已经上线了,你就不能使用debug方式了...
- 聊聊Python自动化脚本部署服务器全流程(详细)
-
来源:AirPython作者:星安果1.前言大家好,我是安果!日常编写的Python自动化程序,如果在本地运行稳定后,就可以考虑将它部署到服务器,结合定时任务完全解放双手但是,由于自动化程序与平...
- 「干货分享」推荐5个可以让你事半功倍的Python自动化脚本
-
作者:俊欣来源:关于数据分析与可视化相信大家都听说自动化流水线、自动化办公等专业术语,在尽量少的人工干预的情况下,机器就可以根据固定的程序指令来完成任务,大大提高了工作效率。今天小编来为大家介绍几个P...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- MVC框架 (46)
- spring框架 (46)
- 框架图 (58)
- flask框架 (53)
- quartz框架 (51)
- abp框架 (47)
- jpa框架 (47)
- springmvc框架 (49)
- 分布式事务框架 (65)
- scrapy框架 (56)
- shiro框架 (61)
- 定时任务框架 (56)
- java日志框架 (61)
- JAVA集合框架 (47)
- mfc框架 (52)
- abb框架断路器 (48)
- ui自动化框架 (47)
- grpc框架 (55)
- ppt框架 (48)
- 内联框架 (52)
- cad怎么画框架 (58)
- ps怎么画框架 (47)
- ssm框架实现登录注册 (49)
- oracle字符串长度 (48)
- oracle提交事务 (47)