royers/phgo
Composer 安装命令:
composer require royers/phgo
包简介
A simple php library for managing http requests based on the net/http package from golang standard library
README 文档
README
A lightweight go-like HTTP library for PHP.
Installation
composer require royers/phgo
Usage
In order to be able to use this package you'll need to use these components:
- A ServeMux object.
- Your own functions which will be injected a Response and Request object by default.
Example on how to register a route
This simple example will show you how to access dynamic parameters and writing response headers.
Let's suppose we have a project that has this structure:
├──public
│ └──index.php
├──src
├──vendor
├──composer.json
├──blogs.json
public/index.php
<?php require_once __DIR__ . "/../vendor/autoload.php"; use Royers\Http\Method; use Royers\Http\{Request, Response, StatusCode}; use Royers\Http\ServeMux; $mux = new ServeMux(); $mux->handleFunc( "/blogs/{id}", Method::Get, function (Response $w, Request $r) { $blogsPath = __DIR__ . "/../blogs.json"; $blogsJson = file_get_contents($blogsPath); $blogs = json_decode($blogsJson, true); // PhGo doesn't validate data for you $blogId = filter_var( $r->dynamicParams["id"], FILTER_VALIDATE_INT, ['default' => 0] ); if ($blogId <= 0) { $w->header()->set("Content-Type", "application/json"); $w->writeHeader(StatusCode::BadRequest); echo json_encode( [ "msg" => "Blog id can't be lower than 1" ] ); return; } $blog = array_find($blogs, fn (array $blog) => $blog['id'] === $blogId); if (!$blog) { $w->header()->set("Content-Type", "application/json"); $w->writeHeader(StatusCode::NotFound); echo json_encode( [ "msg" => "Blog with id = {$blogId} not found" ] ); return; } /* Notice that you must write all the headers you want to send with the header methods, i.e set, add, etc. before throwing any output. These headers will be send when you call the writeHeader method. */ $w->header()->set("Content-Type", "application/json"); $w->writeHeader(StatusCode::Ok); echo json_encode($blog); } ); $mux->listen();
blogs.json
[
{
"id": 1,
"title": "The Rise of Minimalist Web Design",
"author": "Jane Doe",
"date": "2025-01-12",
"tags": ["design", "web", "minimalism"],
"content": "Minimalist design continues to dominate the web, focusing on clarity and user experience."
},
{
"id": 2,
"title": "Understanding Pointers in C",
"author": "John Smith",
"date": "2025-01-20",
"tags": ["programming", "C", "pointers"],
"content": "Pointers are one of the most powerful features of C. In this article, we break down how they work."
}
// More blogs...
]
royers/phgo 适用场景与选型建议
royers/phgo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 08 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 royers/phgo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 royers/phgo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 31
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-07