Mix Vega 发布,支持 Swoole、WorkerMan 的 CLI HTTP 网络框架
ccwgpt 2024-10-10 04:55 26 浏览 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/
相关推荐
- Xtreme套件Xtreme Suite Pro正式发布v17.0.0
-
Codejock软件公司的Xtreme套件(XtremeSuite)包含了三种流行的组件:Xtreme命令工具栏(XtremeCommandBars)——把需要创建的具有改进对接算法的所有组件...
- Wine能不能跑Win程序?信创操作系统下运行Windows应用的条件!
-
原文链接:「链接」Hello,大家好啊,今天给大家带来一篇信创操作系统上使用Wine运行Windows应用程序的条件的文章,欢迎大家分享点赞,点个在看和关注吧!在日常使用国产信创操作系统(如统...
- VC界面开发组件Xtreme Toolkit Pro全新发布v17.0.0
-
Codejock软件公司的XtremeToolkitPro是屡获殊荣的VC界面库,是MFC开发中最全面界面控件套包,它提供了Windows开发所需要的11种主流的VisualC++MFC控件,...
- 机器视觉软件开发新人入门必看 --机器视觉软件开发学习路径
-
机器视觉是机械、运动、控制、光学、软件、算法于一体的交叉学科,对于学工科的人来说,机械、运动、控制都有一定的了解,对于软件、算法、光学不是很了解。一台设备,有一个到二个机械设计师或者结构工程师,那么这...
- 数控变频器的研究与实现(数控变频器的研究与实现思考题)
-
一般变频器具有两种控制方式:控制面板控制方式和串行通信数据控制方式。控制面板控制方式利用变频器自带控制面板进行手动操控,一般应用于非自动控制场合。在自动化程度越来越高的工业生产现场以及机电一体化的数控...
- 实用 | 分享几个非常实用的开源项目
-
前言本次分享几个实用的、值得学习使用的嵌入式相关开源项目,下面列举的这些基本上都在本公众号分享过,详细介绍及使用可查看往期笔记。protobufProtocolBuffers,是Google公司开发...
- Windows桌面应用程序常用开发框架的设计案例全面展示
-
Windows桌面应用程序是我们日常生活中不可或缺的一部分,而开发这些应用程序需要使用相应的框架。本文将全面介绍常用的Windows桌面应用程序开发框架,帮助您了解并选择适合的开发工具。一、原生的Wi...
- .NET9 FCall/QCall调用约定(.net 调用存储过程)
-
蓝字江湖评谈设为关注前言FCall/Qcall是托管与非托管之间的调用约定,双方需要一个契约,以弥合彼此的互相/单向调用。非托管调用约定先了解下非托管约定,一般有四种,分别为thiscall,std...
- BCGControlBar Pro for MFC v24.4正式发布
-
BCGControlBar(BusinessComponentsGalleryControlBar)专业版是MFC的一个扩展库,您可以用来构建类似于MicrosoftOffice2000/X...
- MFC多文档视图(mfc 多文档)
-
你可以因为现任不好而分手,但千万不要认为别人更好,永远有人更好,眼下便是更好。。。----网易云热评一、多文档视图架构程序1、特点:可以管理多个文档。(可以有多个文档类对象)2、相关类CWinA...
- MFC扩展库BCGControlBar Pro v33.5新版亮点:Ribbon Bar等全新升级
-
BCGControlBar库拥有500多个经过全面设计、测试和充分记录的MFC扩展类。我们的组件可以轻松地集成到您的应用程序中,并为您节省数百个开发和调试时间。BCGControlBar专业版v3...
- 山东新华电脑学院4G软件专业明星优秀作品展
-
项目实战工程师:向修艺年龄:18岁班级:4G软件1501班座右铭:付出才会有收获导师寄语:自学能力和实践能力都非常出色,并且学习认真做事责任心强,是不可多得的人才。相信将来如果能获得机会,发挥自己的...
- MFC转QT:Qt基础知识(mfc获取当前日期和时间信息)
-
1.Qt框架概述Qt的历史和版本Qt是一个跨平台的C++应用程序开发框架,由挪威公司Trolltech(现为QtCompany)于1991年创建。Qt的发展历程:1991年:Qt项目启动1995年...
- MFC转QT:Qt高级特性 - 事件系统(mfc读取txt文件每一行数据)
-
Qt事件处理机制Qt的事件系统是整个框架的核心基础之一,负责处理用户输入、窗口系统消息和应用内部的通信。相比MFC的消息映射系统,Qt的事件处理机制更加灵活和直观。基本概念事件(Event)是Qt框...
- MFC用户界面套包BCGControlBar Pro for MFC发布v25.0
-
BCGControlBar(BusinessComponentsGalleryControlBar)专业版是MFC的一个扩展库,您可以用来构建类似于MicrosoftOffice2000/X...
你 发表评论:
欢迎- 一周热门
- 最近发表
-
- Xtreme套件Xtreme Suite Pro正式发布v17.0.0
- Wine能不能跑Win程序?信创操作系统下运行Windows应用的条件!
- VC界面开发组件Xtreme Toolkit Pro全新发布v17.0.0
- 机器视觉软件开发新人入门必看 --机器视觉软件开发学习路径
- 数控变频器的研究与实现(数控变频器的研究与实现思考题)
- 实用 | 分享几个非常实用的开源项目
- Windows桌面应用程序常用开发框架的设计案例全面展示
- .NET9 FCall/QCall调用约定(.net 调用存储过程)
- BCGControlBar Pro for MFC v24.4正式发布
- MFC多文档视图(mfc 多文档)
- 标签列表
-
- MVC框架 (46)
- spring框架 (46)
- 框架图 (58)
- flask框架 (53)
- quartz框架 (51)
- abp框架 (47)
- jpa框架 (47)
- laravel框架 (46)
- springmvc框架 (49)
- 分布式事务框架 (65)
- scrapy框架 (56)
- shiro框架 (61)
- 定时任务框架 (56)
- java日志框架 (61)
- JAVA集合框架 (47)
- mfc框架 (52)
- grpc框架 (55)
- ppt框架 (48)
- 内联框架 (52)
- winform框架 (46)
- cad怎么画框架 (58)
- ps怎么画框架 (47)
- ssm框架实现登录注册 (49)
- oracle字符串长度 (48)
- oracle提交事务 (47)