{"id":20441743,"url":"https://github.com/mix-php/vega","last_synced_at":"2025-08-21T10:31:18.604Z","repository":{"id":47736995,"uuid":"371242863","full_name":"mix-php/vega","owner":"mix-php","description":"💫 Vega is a CLI mode HTTP web framework written in PHP supports Swoole, WorkerMan, FPM, CLI-Server / Vega 是一个用 PHP 编写的 CLI 模式 HTTP 网络框架，支持 Swoole、WorkerMan、FPM、CLI-Server","archived":false,"fork":false,"pushed_at":"2024-01-26T03:46:53.000Z","size":97,"stargazers_count":54,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-26T15:46:48.460Z","etag":null,"topics":["cli","framework","mixphp","php","router","swoole","vega","workerman"],"latest_commit_sha":null,"homepage":"https://openmix.org/mix-php","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mix-php.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-27T04:10:46.000Z","updated_at":"2024-06-18T22:39:50.398Z","dependencies_parsed_at":"2024-01-24T12:27:54.670Z","dependency_job_id":"c12a6966-3bc6-4f8e-b8a6-8c011a4a270d","html_url":"https://github.com/mix-php/vega","commit_stats":{"total_commits":108,"total_committers":5,"mean_commits":21.6,"dds":0.05555555555555558,"last_synced_commit":"934f948dbc95e5a348bbcad65b8fba79146b0f96"},"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fvega","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fvega/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fvega/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fvega/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mix-php","download_url":"https://codeload.github.com/mix-php/vega/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229885862,"owners_count":18139383,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","framework","mixphp","php","router","swoole","vega","workerman"],"created_at":"2024-11-15T09:34:32.526Z","updated_at":"2024-12-19T22:08:13.325Z","avatar_url":"https://github.com/mix-php.png","language":"PHP","readme":"\u003e OpenMix 出品：[https://openmix.org](https://openmix.org/mix-php)\n\n# Mix Vega\n\n中文 | [English](README_EN.md)\n\nVega is a CLI mode HTTP web framework written in PHP support Swoole, WorkerMan, FPM, CLI-Server\n\nVega 是一个用 PHP 编写的 CLI 模式 HTTP 网络框架，支持 Swoole、WorkerMan、FPM、CLI-Server\n\n## 概述\n\nVega 是 [MixPHP](https://github.com/mix-php/mix) `V3+` 内置的最核心的组件 (可独立使用)，参考\ngolang [gin](https://github.com/gin-gonic/gin) [mux](https://github.com/gorilla/mux) 开发，它包含 Web 应用处理的大量功能 (数据库处理除外)\n，包括：路由、渲染、参数获取、中间件、文件上传、静态文件处理等；具有 CLI 模式下强大的兼容性，同时支持 Swoole、WorkerMan、FPM、CLI-Server, 并且支持 Swoole 的多种进程模型与协程。\n\n**推荐搭配以下数据库使用：**\n\n- https://github.com/mix-php/database\n- https://github.com/mix-php/redis\n- https://github.com/top-think/think-orm\n- https://github.com/illuminate/database\n\n**推荐文章：**\n\n- [使用 mix/vega + mix/db 进行现代化的原生 PHP 开发](https://zhuanlan.zhihu.com/p/387493850)\n\n## 技术交流\n\n知乎：https://www.zhihu.com/people/onanying    \n官方QQ群：[284806582](https://shang.qq.com/wpa/qunwpa?idkey=b3a8618d3977cda4fed2363a666b081a31d89e3d31ab164497f53b72cf49968a)\n, [825122875](http://shang.qq.com/wpa/qunwpa?idkey=d2908b0c7095fc7ec63a2391fa4b39a8c5cb16952f6cfc3f2ce4c9726edeaf20)\n敲门暗号：vega\n\n## 安装\n\n\u003e 需先安装 [Swoole](https://wiki.swoole.com/#/environment) 或者 [WorkerMan](http://doc.workerman.net/install/requirement.html)\n\n```\ncomposer require mix/vega\n```\n\n## 快速开始\n\n- Swoole 多进程 (异步) 中使用\n\n```php\n\u003c?php\nrequire __DIR__ . '/vendor/autoload.php';\n\n$vega = new Mix\\Vega\\Engine();\n$vega-\u003ehandle('/hello', function (Mix\\Vega\\Context $ctx) {\n    $ctx-\u003estring(200, 'hello, world!');\n})-\u003emethods('GET');\n\n$http = new Swoole\\Http\\Server('0.0.0.0', 9501);\n$http-\u003eon('Request', $vega-\u003ehandler());\n$http-\u003eset([\n    'worker_num' =\u003e 4,\n]);\n$http-\u003estart();\n```\n\n开启多进程协程\n\n```php\n$http-\u003eon('Request', $vega-\u003ehandler());\n$http-\u003eon('WorkerStart', function ($server, $workerId) {\n    // 协程初始化\n    // 比如：启动 mix/database mix/redis 的连接池\n});\n$http-\u003eset([\n    'enable_coroutine' =\u003e true,\n    'worker_num' =\u003e 4,\n]);\n```\n\n```\nphp swoole.php\n```\n\n- Swoole 单进程 (协程) 中使用\n\n```php\n\u003c?php\nrequire __DIR__ . '/vendor/autoload.php';\n\nSwoole\\Coroutine\\run(function () {\n    $vega = new Mix\\Vega\\Engine();\n    $vega-\u003ehandle('/hello', function (Mix\\Vega\\Context $ctx) {\n        $ctx-\u003estring(200, 'hello, world!');\n    })-\u003emethods('GET');\n    \n    $server = new Swoole\\Coroutine\\Http\\Server('0.0.0.0', 9502, false);\n    $server-\u003ehandle('/', $vega-\u003ehandler());\n    $server-\u003estart();\n});\n```\n\n```\nphp swooleco.php\n```\n\n- WorkerMan 中使用\n\n```php\n\u003c?php\nrequire __DIR__ . '/vendor/autoload.php';\n\n$vega = new Mix\\Vega\\Engine();\n$vega-\u003ehandle('/hello', function (Mix\\Vega\\Context $ctx) {\n    $ctx-\u003estring(200, 'hello, world!');\n})-\u003emethods('GET');\n\n$http_worker = new Workerman\\Worker(\"http://0.0.0.0:2345\");\n$http_worker-\u003eonMessage = $vega-\u003ehandler();\n$http_worker-\u003ecount = 4;\nWorkerman\\Worker::runAll();\n```\n\n```\nphp wokerman.php start\n```\n\n- PHP-FPM 中使用\n\n在 `nginx` 配置 `rewrite` 重写到 `index.php`\n\n```php\n\u003c?php\nrequire __DIR__ . '/vendor/autoload.php';\n\n$vega = new Mix\\Vega\\Engine();\n$vega-\u003ehandle('/hello', function (Mix\\Vega\\Context $ctx) {\n    $ctx-\u003estring(200, 'hello, world!');\n})-\u003emethods('GET');\nreturn $vega-\u003erun();\n```\n\n- PHP [cli-server](https://www.php.net/manual/zh/features.commandline.webserver.php) 中使用\n\n这个内置的Web服务器主要用于本地开发使用，不可用于线上产品环境。\n\n```php\n\u003c?php\nrequire __DIR__ . '/vendor/autoload.php';\n\n$vega = new Mix\\Vega\\Engine();\n$vega-\u003ehandle('/hello', function (Mix\\Vega\\Context $ctx) {\n    $ctx-\u003estring(200, 'hello, world!');\n})-\u003emethods('GET');\nreturn $vega-\u003erun();\n```\n\n```\nphp -S localhost:8000 router.php\n```\n\n- 访问测试\n\n~~~\n% curl http://127.0.0.1:9501/hello\nhello, world!\n~~~\n\n## 路由配置\n\n配置 `Closure` 闭包路由\n\n```php\n$vega = new Mix\\Vega\\Engine();\n$vega-\u003ehandle('/hello', function (Mix\\Vega\\Context $ctx) {\n    $ctx-\u003estring(200, 'hello, world!');\n})-\u003emethods('GET');\n```\n\n配置 `callable` 路由\n\n```php\nclass Hello {\n    public function index(Mix\\Vega\\Context $ctx) {\n        $ctx-\u003estring(200, 'hello, world!');\n    }\n}\n$vega = new Mix\\Vega\\Engine();\n$vega-\u003ehandle('/hello', [new Hello(), 'index'])-\u003emethods('GET');\n```\n\n配置路由变量\n\n```php\n$vega = new Mix\\Vega\\Engine();\n$vega-\u003ehandle('/users/{id:\\d+}', function (Mix\\Vega\\Context $ctx) {\n    $id = $ctx-\u003eparam('id');\n    $ctx-\u003estring(200, 'hello, world!');\n})-\u003emethods('GET');\n```\n\n配置多个 `method`\n\n```php\n$vega = new Mix\\Vega\\Engine();\n$vega-\u003ehandle('/hello', function (Mix\\Vega\\Context $ctx) {\n    $ctx-\u003estring(200, 'hello, world!');\n})-\u003emethods('GET', 'POST');\n```\n\n## 路由前缀 (分组)\n\n```php\n$vega = new Mix\\Vega\\Engine();\n$foo = $vega-\u003epathPrefix('/foo');\n$bar = $foo-\u003epathPrefix('/bar');\n$bar-\u003ehandle('/baz', function (Mix\\Vega\\Context $ctx) { // path=/foo/bar/baz\n    $ctx-\u003estring(200, 'hello, world!');\n})-\u003emethods('GET');\n```\n\n## 参数获取\n\n### 请求参数\n\n|  方法名称   | 描述  |\n|  ----  | ----  |\n| $ctx-\u003erequest: ServerRequestInterface | 符合PSR的请求对象 |\n| $ctx-\u003eresponse: ResponseInterface | 符合PSR的响应对象 |\n| $ctx-\u003eparam(string $key): string  | 获取路由参数 |\n| $ctx-\u003equery(string $key): string  | 获取url参数，包含路由参数 |\n| $ctx-\u003edefaultQuery(string $key, string $default): string  | 获取url参数，可配置默认值 |\n| $ctx-\u003egetQuery(string $key): string or null  | 获取url参数, 可判断是否存在 |\n| $ctx-\u003epostForm(string $key): string  | 获取post参数 |\n| $ctx-\u003edefaultPostForm(string $key, string $default): string  | 获取post参数，可配置默认值 |\n| $ctx-\u003egetPostForm(string $key): string or null  | 获取post参数，可判断是否存在 |\n\n### Headers, Cookies, Uri ...\n\n|  方法名称   | 描述  |\n|  ----  | ----  |\n| $ctx-\u003emethod(): string  | 请求类型 |\n| $ctx-\u003econtentType(): string  | 文档类型 |\n| $ctx-\u003eheader(string $key): string  | 请求头 |\n| $ctx-\u003ecookie(string $name): string  | cookies |\n| $ctx-\u003euri(): UriInterface  | 完整uri |\n| $ctx-\u003erawData(): string  | 原始包数据 |\n\n### 客户端IP\n\n|  方法名称   | 描述  |\n|  ----  | ----  |\n| $ctx-\u003eclientIP(): string  | 从反向代理获取用户真实IP |\n| $ctx-\u003eremoteIP(): string  | 获取远程IP |\n\n## 上传文件处理\n\n|  方法名称   | 描述  |\n|  ----  | ----  |\n| $ctx-\u003eformFile(string $name): UploadedFileInterface  | 获取上传的第一个文件 |\n| $ctx-\u003emultipartForm(): UploadedFileInterface[]  | 获取上传的全部文件 |\n\n文件保存\n\n```php\n$file = $ctx-\u003eformFile('img');\n$targetPath = '/data/project/public/uploads/' . $file-\u003egetClientFilename();\n$file-\u003emoveTo($targetPath);\n```\n\n## 请求上下文\n\n请求当中需要保存一些信息，比如：会话、JWT载荷等。\n\n|  方法名称   | 描述  |\n|  ----  | ----  |\n| $ctx-\u003eset(string $key, $value): void  | 设置值 |\n| $ctx-\u003eget(string $key): mixed or null  | 获取值 |\n| $ctx-\u003emustGet(string $key): mixed or throws  | 获取值或抛出异常 |\n\n## 中断执行\n\n`abort` 执行后，会停止执行后面的全部代码，包括中间件。\n\n|  方法名称   | 描述  |\n|  ----  | ----  |\n| $ctx-\u003eabort(): void  | 中断，需自行处理响应 |\n| $ctx-\u003eabortWithStatus(int $code): void  | 中断并响应状态码 |\n| $ctx-\u003eabortWithStatusJSON(int $code, $data): void  | 中断并响应JSON |\n\n```php\n$vega = new Mix\\Vega\\Engine();\n$vega-\u003ehandle('/users/{id}', function (Mix\\Vega\\Context $ctx) {\n    if (true) {\n        $ctx-\u003estring(401, 'Unauthorized');\n        $ctx-\u003eabort();\n    }\n    $ctx-\u003estring(200, 'hello, world!');\n})-\u003emethods('GET');\n```\n\n## 响应处理\n\n|  方法名称   | 描述  |\n|  ----  | ----  |\n| $ctx-\u003estatus(int $code): void  | 设置状态码 |\n| $ctx-\u003esetHeader(string $key, string $value): void  | 设置header |\n| $ctx-\u003esetCookie(string $name, string $value, int $expire = 0, ...): void  | 设置cookie |\n| $ctx-\u003eredirect(string $location, int $code = 302): void  | 重定向 |\n\n### 重定向\n\n```php\n$ctx-\u003eredirect('https://www.baidu.com/');\n$ctx-\u003eabort();\n```\n\n## JSON 请求与输出\n\n获取 JSON 请求数据\n\n```php\n$vega = new Mix\\Vega\\Engine();\n$vega-\u003ehandle('/users', function (Mix\\Vega\\Context $ctx) {\n    $obj = $ctx-\u003egetJSON();\n    if (!$obj) {\n        throw new \\Exception('Parameter error');\n    }\n    var_dump($obj);\n    $ctx-\u003eJSON(200, [\n        'code' =\u003e 0,\n        'message' =\u003e 'ok'\n    ]);\n})-\u003emethods('POST');\n```\n\n`mustGetJSON` 自带有效性检查，以下代码等同于上面\n\n```php\n$vega = new Mix\\Vega\\Engine();\n$vega-\u003ehandle('/users', function (Mix\\Vega\\Context $ctx) {\n    $obj = $ctx-\u003emustGetJSON();\n    var_dump($obj);\n    $ctx-\u003eJSON(200, [\n        'code' =\u003e 0,\n        'message' =\u003e 'ok'\n    ]);\n})-\u003emethods('POST');\n```\n\n### JSONP 处理\n\n```php\n$vega = new Mix\\Vega\\Engine();\n$vega-\u003ehandle('/jsonp', function (Mix\\Vega\\Context $ctx) {\n    $ctx-\u003eJSONP(200, [\n        'code' =\u003e 0,\n        'message' =\u003e 'ok'\n    ]);\n})-\u003emethods('GET');\n```\n\n## HTML 视图渲染\n\n创建视图文件 `foo.php`\n\n```php\n\u003cp\u003eid: \u003c?= $id ?\u003e, name: \u003c?= $name ?\u003e\u003c/p\u003e\n\u003cp\u003efriends:\u003c/p\u003e\n\u003cul\u003e\n    \u003c?php foreach($friends as $name): ?\u003e\n        \u003cli\u003e\u003c?= $name ?\u003e\u003c/li\u003e\n    \u003c?php endforeach; ?\u003e\n\u003c/ul\u003e\n```\n\n导入其他视图\n\n```php\n\u003c?= $this-\u003erender('header', $__data__); ?\u003e\n```\n\n配置视图路径，并响应html\n\n```php\n$vega = new Mix\\Vega\\Engine();\n$vega-\u003ewithHTMLRoot('/data/project/views');\n$vega-\u003ehandle('/html', function (Mix\\Vega\\Context $ctx) {\n    $ctx-\u003eHTML(200, 'foo', [\n        'id' =\u003e 1000,\n        'name' =\u003e '小明',\n        'friends' =\u003e [\n            '小花',\n            '小红'\n        ]\n    ]);\n})-\u003emethods('GET');\n```\n\n## 静态文件处理\n\n基于 `sendfile` 零拷贝，不支持在 `PHP-FPM` 中使用\n\n```php\n$vega = new Mix\\Vega\\Engine();\n$vega-\u003estatic('/static', '/data/project/public/static');\n$vega-\u003estaticFile('/favicon.ico', '/data/project/public/favicon.ico');\n```\n\n## 设置中间件\n\n给某个路由配置中间件，可配置多个\n\n```php\n$vega = new Mix\\Vega\\Engine();\n$func = function (Mix\\Vega\\Context $ctx) {\n    // do something\n    $ctx-\u003enext();\n};\n$vega-\u003ehandle('/hello', $func, function (Mix\\Vega\\Context $ctx) {\n    $ctx-\u003estring(200, 'hello, world!');\n})-\u003emethods('GET');\n```\n\n配置全局中间件\n\n- 全局中间件：即便没有匹配到路由也会执行\n\n```php\n$vega = new Mix\\Vega\\Engine();\n$vega-\u003euse(function (Mix\\Vega\\Context $ctx) {\n    $ctx-\u003enext();\n});\n```\n\n- 路由前缀全局中间件\n\n```php\n$vega = new Mix\\Vega\\Engine();\n$foo = $vega-\u003epathPrefix('/foo');\n$foo-\u003euse(function (Mix\\Vega\\Context $ctx) {\n    $ctx-\u003enext();\n});\n```\n\n前置中间件\n\n```php\n$vega-\u003euse(function (Mix\\Vega\\Context $ctx) {\n    // do something\n    $ctx-\u003enext();\n});\n```\n\n后置中间件\n\n```php\n$vega-\u003euse(function (Mix\\Vega\\Context $ctx) {\n    $ctx-\u003enext();\n    // do something\n});\n```\n\n### 404 自定义\n\n```php\n$vega = new Mix\\Vega\\Engine();\n$vega-\u003euse(function (Mix\\Vega\\Context $ctx) {\n    try{\n        $ctx-\u003enext();\n    } catch (Mix\\Vega\\Exception\\NotFoundException $ex) {\n        $ctx-\u003estring(404, 'New 404 response');\n        $ctx-\u003eabort();\n    }\n});\n```\n\n### 500 全局异常捕获\n\n```php\n$vega = new Mix\\Vega\\Engine();\n$vega-\u003euse(function (Mix\\Vega\\Context $ctx) {\n    try{\n        $ctx-\u003enext();\n    } catch (\\Throwable $ex) {\n        if ($ex instanceof Mix\\Vega\\Abort || $ex instanceof Mix\\Vega\\Exception\\NotFoundException) {\n            throw $ex;\n        }\n        $ctx-\u003estring(500, 'New 500 response');\n        $ctx-\u003eabort();\n    }\n});\n```\n\n## License\n\nApache License Version 2.0, http://www.apache.org/licenses/\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmix-php%2Fvega","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmix-php%2Fvega","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmix-php%2Fvega/lists"}