thruster / http-router
Thruster HttpRouter Component (PSR-7 and FastRoute based router)
1.1.0
2016-05-31 12:04 UTC
Requires
- php: >=7.0
- nikic/fast-route: >=0.7.0,<1.0.0
- psr/http-message: ~1.0
- thruster/promise: ^1.0
Requires (Dev)
- phpunit/phpunit: ~5.0
This package is auto-updated.
Last update: 2024-11-14 03:11:35 UTC
README
[] (https://github.com/ThrusterIO/http-router/releases) [] (LICENSE) [] (https://travis-ci.org/ThrusterIO/http-router) [] (https://scrutinizer-ci.com/g/ThrusterIO/http-router) [] (https://scrutinizer-ci.com/g/ThrusterIO/http-router) [] (https://packagist.org/packages/thruster/http-router)
The Thruster HttpRoute Component. PSR-7 and FastRoute based simple router.
Install
Via Composer
$ composer require thruster/http-router
Usage
Standalone usage
<?php use Psr\Http\Message\RequestInterface; use Thruster\Component\HttpRouter\Router; use Thruster\Component\HttpRouter\RouteProvider; $application = new class implements RouteProvider { public function getRoutes() : array { return [ 'hello_world' => ['GET', '/', 'hello'], ['POST', '/', [$this, 'foo']] ]; } public function hello(ServerRequestInterface $request) { // return new Response(200, [], 'Hello world'); } public function foo(ServerRequestInterface $request) { // return new Response(404, [], 'Foo Bar'); } }; $router = new Router($application); $response = $router->handleRequest(ServerRequest::fromGlobals()); // PSR-7 Response
PSR-7 style middleware
<?php use Psr\Http\Message\RequestInterface; use Thruster\Component\HttpRouter\Router; use Thruster\Component\HttpRouter\RouteProvider; use Thruster\Component\HttpRouter\RouteHandler; $application = new class implements RouteProvider, RouteHandler { public function getRoutes() : array { return [ 'hello_world' => ['GET', '/', 'hello'], ['POST', '/', [$this, 'foo']] ]; } public function handleRoute( ServerRequestInterface $request, ResponseInterface $response, callable $actionHandler ) : ResponseInterface { // ... call actionHandler and return ResponseInterface } public function handleRouteMethodNotAllowed( ServerRequestInterface $request, ResponseInterface $response, array $allowedMethods ) : ResponseInterface { // ... handle method not allowed error } public function handleRouteNotFound( ServerRequestInterface $request, ResponseInterface $response ) : ResponseInterface { // ... handle route not found (404) } }; $router = new Router($application, $application); $response = $router(ServerRequest::fromGlobals(), new Response()); // PSR-7 Response
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CONDUCT for details.
License
Please see License File for more information.