n1215 / http-router
A PSR-7 / PSR-15 compatible router interface and a PSR-15 server request handler implementation for routing.
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-11-11 21:19:35 UTC
README
A PSR-7 / PSR-15 compatible router interface and a PSR-15 server request handler implementation for routing.
This package helps you to create your own http router.
Usage
<?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);
Implementation examples
- Router -> n1215/hakudo
- RoutingErrorResponder -> n1215/tsukuyomi-demo