tourze/psr15-chain-request-handler
最新稳定版本:0.0.2
Composer 安装命令:
composer require tourze/psr15-chain-request-handler
包简介
PSR-15 Chain Request Handler
README 文档
README
Introduction
A PSR-15 compatible chain request handler for PHP. This package allows you to combine multiple request handlers in a chain, processing HTTP requests sequentially until a handler returns a non-404 response. If all handlers return 404, a final 404 response is returned.
Features
- Chain multiple PSR-15 request handlers
- Automatically skips handlers that return 404
- Simple API for adding handlers dynamically
- Fully PSR-15 compatible
- Well-tested and easy to integrate
Installation
Requirements:
- PHP >= 8.1
- psr/http-message ^1.1|^2.0
- psr/http-server-handler ^1.0
- nyholm/psr7 ^1.8.2
Install via Composer:
composer require tourze/psr15-chain-request-handler
Quick Start
use Nyholm\Psr7\ServerRequest; use Nyholm\Psr7\Response; use Tourze\PSR15ChainRequestHandler\ChainRequestHandler; use Psr\Http\Server\RequestHandlerInterface; // Example handler that always returns 404 class NotFoundHandler implements RequestHandlerInterface { public function handle($request): Response { return new Response(404, body: 'Not Found'); } } // Example handler that returns 200 for a specific path class HelloHandler implements RequestHandlerInterface { public function handle($request): Response { if ($request->getUri()->getPath() === '/hello') { return new Response(200, body: 'Hello World'); } return new Response(404, body: 'Not Found'); } } $chain = new ChainRequestHandler([ new NotFoundHandler(), new HelloHandler(), ]); $request = new ServerRequest('GET', '/hello'); $response = $chain->handle($request); // $response->getStatusCode() === 200
Documentation
- API Reference
- See
tests/ChainRequestHandlerTest.phpfor usage scenarios
Advanced Usage
- Add handlers dynamically with
$chain->addHandler($handler); - Handlers are processed in the order they are added
- If no handler returns a non-404 response, the chain returns a 404 with body 'Not Found'
Contributing
- Please submit issues or pull requests via GitHub
- Follow PSR coding standards
- Run tests with PHPUnit before submitting PRs
License
MIT License. See LICENSE for details.
Changelog
See the CHANGELOG for release history and upgrade notes.
统计信息
- 总下载量: 163
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-24