Mix Vega 发布,支持 Swoole、WorkerMan 的 CLI HTTP 网络框架
ccwgpt 2024-10-10 04:55 20 浏览 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/
相关推荐
- 盲盒小程序背后的技术揭秘:如何打造个性化购物体验
-
在2025年的今天,盲盒小程序作为一种新兴的购物方式,正以其独特的魅力和个性化体验吸引着越来越多的消费者。这种将线上购物与盲盒概念相结合的应用,不仅为消费者带来了未知的惊喜,还通过一系列技术手段实现了...
- 小程序·云开发已支持单日亿级调用量,接口可用率高达99.99%
-
2019-10-1914:1210月19日,由腾讯云与微信小程序团队联合举办的“小程序·云开发”技术峰会在北京召开。会上,微信小程序团队相关负责人表示“小程序·云开发”系统架构已经支持每天亿级别的...
- 程序员副业开启模式:8个GitHub上可以赚钱的小程序
-
前言开源项目作者:JackonYang今天推荐的这个项目是「list-of-wechat-mini-program-list」,开源微信小程序列表的列表、有赚钱能力的小程序开源代码。这个项目分为两部分...
- 深度科普:盲盒小程序开发的底层逻辑
-
在当下的数字化浪潮中,盲盒小程序以其独特的趣味性和互动性,吸引着众多消费者的目光。无论是热衷于收集玩偶的年轻人,还是享受拆盒惊喜的上班族,都对盲盒小程序情有独钟。那么,这种备受欢迎的盲盒小程序,其开发...
- 微信小程序的制作步骤
-
SaaS小程序制作平台,作为数字化转型时代下的创新产物,不仅将易用性置于设计的核心位置,让非技术背景的用户也能轻松上手,快速制作出功能丰富、界面精美的小程序,更在性能和稳定性方面投入了大量精力,以确保...
- 携程开源--小程序构建工具,三分钟搞定
-
前言今天推荐的这个项目是「wean」,一个小程序构建打包工具。在wean之前,大量小程序工具使用webpack进行打包,各种loader、plugin导致整个开发链路变长。wean旨在解...
- 校园小程序的搭建以及营收模式校园外卖程序校园跑腿校园圈子系统
-
校园小程序的架构设计主要包括云端架构和本地架构两部分。云端架构方面,采用Serverless架构可以降低技术门槛,通过阿里云、腾讯云等平台提供的云服务,可以实现弹性扩容和快速部署。例如,使用云数据库、...
- 盲盒小程序开发揭秘:技术架构与实现原理全解析
-
在2025年的今天,盲盒小程序作为一种结合了线上购物与趣味性的创新应用,正受到越来越多用户的喜爱。其背后的技术架构与实现原理,对于想要了解或涉足这一领域的人来说,无疑充满了神秘与吸引力。本文将为大家科...
- 月活百万的小程序架构设计:流量暴增秘籍
-
从小程序到"大"程序的蜕变之路当你的小程序用户量从几千跃升至百万级别时,原有的架构就像一件不合身的衣服,处处紧绷。这个阶段最常遇到的噩梦就是服务器崩溃、接口超时、数据丢失。想象一下,在...
- 认知智能如何与产业结合?专家学者共探理论框架与落地实践
-
当前,以大模型为代表的生成式人工智能等前沿技术加速迭代,如何将认知智能与产业结合,成为摆在各行各业面前的一个问题。论坛现场。主办方供图7月4日,2024世界人工智能大会暨人工智能全球治理高级别会议在...
- 现代中医理论框架
-
...
- 认知行为(CBT)中的ABC情绪理论
-
情绪ABC理论是由美国心理学家阿尔伯特·艾利斯(AlbertEllis1913-2007)创建的理论,A表示诱发性事件(Activatingevent),B表示个体针对此诱发性事件产生的一些信...
- 说说卡伦霍妮的理论框架,对你调整性格和人际关系,价值很大
-
01自在今天我主要想说下霍妮的理论框架。主要说三本书,第一本是《我们时代的神经症人格》,第二本是《我们内心的冲突》,第三本是《神经症与人的成长》。根据我的经验,三本书价值巨大,但并不是每个人都能读进去...
- 供应链管理-理论框架
-
一个最佳价值的供应链,应该是一个具有敏捷性、适应性和联盟功能(3A)的供应链,其基本要素包括战略资源、物流管理、关系管理以及信息系统,目标是实现速度、质量、成本、柔性的竞争优势。篇幅有...
- 微信WeUI设计规范文件下载及使用方法
-
来人人都是产品经理【起点学院】,BAT实战派产品总监手把手系统带你学产品、学运营。WeUI是一套同微信原生视觉体验一致的基础样式库,由微信官方设计团队为微信Web开发量身设计,可以令用户的使用感知...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- MVC框架 (46)
- spring框架 (46)
- 框架图 (58)
- bootstrap框架 (43)
- flask框架 (53)
- quartz框架 (51)
- abp框架 (47)
- jpa框架 (47)
- laravel框架 (46)
- express框架 (43)
- scrapy框架 (52)
- beego框架 (42)
- java框架spring (43)
- grpc框架 (55)
- 前端框架bootstrap (42)
- orm框架有哪些 (43)
- ppt框架 (48)
- 内联框架 (52)
- winform框架 (46)
- gui框架 (44)
- cad怎么画框架 (58)
- ps怎么画框架 (47)
- ssm框架实现登录注册 (49)
- oracle字符串长度 (48)
- oracle提交事务 (47)