codesaur/http-application
Composer 安装命令:
composer require codesaur/http-application
包简介
PSR-7 & PSR-15 нийцсэн хөнгөн, уян хатан HTTP Application цөм
关键字:
README 文档
README
PSR-7 & PSR-15 нийцсэн хөнгөн, уян хатан HTTP Application цөм Lightweight, flexible HTTP Application core compliant with PSR-7 & PSR-15
Агуулга / Table of Contents
- Монгол | 2. English | 3. Getting Started
1. Монгол тайлбар
codesaur/http-application нь PSR-7 (HTTP Message) ба PSR-15 (HTTP Server RequestHandler/Middleware) стандартууд дээр суурилсан минималист, өндөр уян хатан, middleware суурьтай Application цөм юм.
Та хүсвэл:
- Router ашиглах (нэг эсвэл олон)
- Middleware удирдах
- Controller/action ашиглах
- Closure route ашиглах
- Exception handler бүртгэх
- Custom request attributes ашиглах
гэх мэтээр өөрийн хүссэн бүтэцтэй web application-ийг хэдхэн мөр кодоор босгох боломжтой.
Гол боломжууд
- PSR-7 стандартын ServerRequest + Response
- PSR-15 Middleware & RequestHandler гинжин бүтэц
- Олон Router-ийг нэг Application-д нэгтгэх (multi-router delegation)
- Application-ийг URL prefix-д mount хийх (Router-ууд reusable)
- Controller суурь класс (сонголтот - controller/action хэв маягт ашиглаж болно)
- Per-route middleware (MiddlewareInterface, Closure, class-string)
- Exception Handler (development mode-той)
- Хэт хөнгөн, хурдан - magic API байхгүй, цэвэр separation of concerns
Дэлгэрэнгүй мэдээлэл
- Бүрэн танилцуулга - Суурилуулалт, хэрэглээ, жишээнүүд
- API тайлбар - Бүх метод, exception-үүдийн тайлбар
- Шалгалтын тайлан - Код шалгалтын тайлан
2. English Description
codesaur/http-application is a minimalist, highly flexible, middleware-based Application core built on PSR-7 (HTTP Message) and PSR-15 (HTTP Server RequestHandler/Middleware) standards.
You can:
- Use Router (one or many)
- Manage Middleware
- Use Controller/action
- Use Closure routes
- Register Exception handler
- Use Custom request attributes
and build your desired web application structure with just a few lines of code.
Key Features
- PSR-7 Standard ServerRequest + Response
- PSR-15 Middleware & RequestHandler Chain Structure
- Multi-router delegation (combine multiple Routers in one Application)
- Mount Application at a URL prefix (Routers are reusable)
- Controller base class (optional - for controller/action style code if you want)
- Per-route middleware (MiddlewareInterface, Closure, class-string)
- Exception Handler (with development mode)
- Extremely lightweight and fast - no magic API, clean separation of concerns
Documentation
- Full Documentation - Installation, usage, examples
- API Reference - Complete API documentation
- Review - Complete package review and code quality assessment
3. Getting Started
Requirements
- PHP 8.2.1+
- Composer
- PSR-7 compatible HTTP Message implementation (e.g.,
codesaur/http-message) - supplies theServerRequestInterfacepassed tohandle()and theResponseInterfaceprototype passed to theApplicationconstructor
Installation
Composer ашиглан суулгана / Install via Composer:
composer require codesaur/http-application
Quick Examples
Application - Basic Setup
use codesaur\Router\Router; use codesaur\Http\Application\Application; use codesaur\Http\Application\ExceptionHandler; use codesaur\Http\Message\ServerRequest; use codesaur\Http\Message\NonBodyResponse; // Router-д route бүртгэх / Register routes on Router $router = new Router(); $router->GET('/', function ($req) { echo 'Hello World!'; }); // Application үүсгэх (fallback хариуны prototype дамжуулна) + middleware + router нэмэх $app = new Application(new NonBodyResponse()); $app->use(new ExceptionHandler()); $app->use($router); // Хүсэлт боловсруулах / Handle request $request = (new ServerRequest())->initFromGlobal(); $response = $app->handle($request);
Router - Dynamic Routes
$router = new Router(); // Төрөлтэй параметртэй нэртэй route $router->GET('/user/{int:id}', [UserController::class, 'show'])->name('user.show'); // Олон method-тэй route $router->POST_PUT('/api/users', [UserController::class, 'save']); // Параметртэй Closure route $router->GET('/sum/{int:a}/{uint:b}', function ($req) { $params = $req->getAttribute('params'); echo $params['a'] + $params['b']; }); $app = new Application(new NonBodyResponse()); $app->use($router);
Mount - Application-ийг URL prefix-д суулгах / Mounting Application at a URL Prefix
use codesaur\Router\Router; // Router-ууд prefix-ийг мэдэхгүй - reusable $adminRouter = new Router(); $adminRouter->GET('/users', [UserAdmin::class, 'list'])->name('users'); $adminRouter->GET('/posts', [PostAdmin::class, 'list'])->name('posts'); // Application-ийг entry point дээр mount хийнэ $app = new Application(new NonBodyResponse()); $app->use($adminRouter); $app->mount('/dashboard'); // бүх route /dashboard prefix-тэй болов // match: /dashboard/users -> Router-д /users-аар тааран ажиллана // generate('users') -> '/dashboard/users' // $app->mount('/admin') гэвэл нэг ч route өөрчлөхгүйгээр /admin-руу шилжинэ
Mount хийсний дараа $req->getAttribute('application')->generate('name') нь Application-руу заана учир Controller-ээс URL үүсгэхэд mount prefix автоматаар нэмэгдэнэ.
Multi-Router - Олон Router нэгтгэх / Combining Multiple Routers
use codesaur\Router\Router; // Module бүрд өөрийн router-тэй $apiRouter = new Router(); $apiRouter->GET('/api/users', [UserApi::class, 'list'])->name('api.users'); $adminRouter = new Router(); $adminRouter->GET('/admin/dashboard', [Admin::class, 'index'])->name('admin.dash'); $homeRouter = new Router(); $homeRouter->GET('/', $homeHandler); // Application-д бүгдийг нэгтгэх $app = new Application(new NonBodyResponse()); $app->use($apiRouter); $app->use($adminRouter); $app->use($homeRouter); // Match order: use() дарааллаар (first-added-wins) - предиктабл default // generate()/pattern() бүх router дээр first-found-wins хайна $url = $app->generate('api.users'); // '/api/users'
Route override - өмнө бүртгэсэн route-ийг зориудаар дарж бичих
// Router бүртгэх $app->use(new ProfileRouter()); // /profile -> ProfileController // Developer-ийн theme controller-руу шилжүүлэх override $themeProfile = new Router(); $themeProfile->GET('/profile', [ThemeProfileController::class, 'show'])->name('profile'); $app->override($themeProfile); // ил override - энэ ялна // Override lane нь ердийн router-ээс өмнө шалгагдана. Зүгээр use()-ийн // дараалалд найдаж далд override хийхгүй - bootstrap дотор ил харагдана.
Controller - controller/action route
use codesaur\Http\Application\Controller; class UserController extends Controller { public function show(int $id): void { $query = $this->getQueryParams(); $page = $query['page'] ?? 1; echo "User ID: $id, Page: $page"; } public function create(): void { $data = $this->getParsedBody(); $name = $data['name'] ?? 'Unknown'; echo "Created user: $name"; } }
Middleware - PSR-15 & Closure
// PSR-15 Middleware class AuthMiddleware implements MiddlewareInterface { public function process($req, $handler): ResponseInterface { // Баталгаажуулалт шалгах / Check authentication if (!$this->isAuthenticated($req)) { return new Response(401); } return $handler->handle($req); } } $app->use(new AuthMiddleware()); // Closure Middleware $app->use(function ($req, $handler) { $startTime = microtime(true); $response = $handler->handle($req); $duration = microtime(true) - $startTime; error_log("Request took: {$duration}s"); return $response; });
Running Tests
Тест ажиллуулах / Run tests:
# Бүх тестүүдийг ажиллуулах / Run all tests composer test # Зөвхөн unit тест / Unit tests only composer test:unit # Зөвхөн integration тест / Integration tests only composer test:integration # Coverage-тэй тест ажиллуулах / Run tests with coverage composer test:coverage
Architecture
Application
+-- Middleware stack (PSR-15 + Closure)
+-- Override lane (list of RouterInterface) <- checked first, explicit override()
| +-- Override Router #1
| +-- ...
+-- Router collection (list of RouterInterface) <- normal use(), first-added-wins
| +-- Router #1
| +-- Router #2
| +-- ...
+-- Mount path (optional URL prefix)
+-- ExceptionHandler
+-- Controller / Closure route executor
Request Flow:
Request
-> Global middleware chain (PSR-15 onion model)
-> Application::match() [strips mount prefix]
-> Override lane, then first Router that matches wins (first-added-wins)
-> Per-route middleware chain (PSR-15 onion model)
-> Controller/action OR Closure
-> Response
URL Generation: Application::generate('name') -> finds name in first Router that has it -> prepends mount prefix -> returns URL.
Changelog
- CHANGELOG.md - Full version history
Contributing & Security
License
This project is licensed under the MIT License.
Author
Narankhuu
codesaur@gmail.com
https://github.com/codesaur
codesaur ecosystem: https://codesaur.net
codesaur/http-application 适用场景与选型建议
codesaur/http-application 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.03k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「router」 「application」 「psr-7」 「psr-15」 「http application」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 codesaur/http-application 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 codesaur/http-application 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 codesaur/http-application 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Kinikit - PHP Application development framework MVC component
A Laravel queue connector to process jobs at the end of the application
Router
PMVC App Action Router
C3JS By phpanonymous (Mahmoud Ibrahim)
统计信息
- 总下载量: 1.03k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 15
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-15