n1215 / http-router
一个PSR-7/PSR-15兼容的路由接口以及路由的PSR-15服务器请求处理器实现。
v2.0.0
2019-06-19 10:25 UTC
Requires
- php: >=7.1.0
- psr/http-server-handler: ^1.0
Requires (Dev)
- phpunit/phpunit: ^7.0
- zendframework/zend-diactoros: ^2.0
This package is auto-updated.
Last update: 2024-09-11 21:02:32 UTC
README
一个PSR-7/PSR-15兼容的路由接口以及路由的PSR-15服务器请求处理器实现。
此包可以帮助您创建自己的HTTP路由器。
用法
<?php // 1. Implement RouterInterface. You can use RoutingResultFactory concrete classes to create RoutingResult. class YourRouter implements N1215\Http\Router\RouterInterface { public function match(ServerRequestInterface $request) : RoutingResultInterface { // implement } } // 2. Implement RoutingErrorResponderInterface. class YourRoutingErrorResponder implements N1215\Http\Router\Handler\RoutingErrorResponderInterface { public function supports(RoutingException $exception): bool { // implement } public function respond(RoutingException $exception, ServerRequestInterface $request): ResponseInterface { // implement } } // 3. Configure to inject them into RoutingHandler. $routingHandler = new N1215\Http\Router\Handler\RoutingHandler( new YourRouter(), new YourNotFoundErrorResponder(), new YourMethodNotAllowedErrorResponder() ); // 4. Use RoutingHandler as an implementation of PSR-15 server request handler. /** @var \Psr\Http\Message\ServerRequestInterface $request */ /** @var \Psr\Http\Message\ResponseInterface $response */ $response = $routingHandler->handle($request);
实现示例
- Router -> n1215/hakudo
- RoutingErrorResponder -> n1215/tsukuyomi-demo