wandu / router
This package is abandoned and no longer maintained.
No replacement package was suggested.
FastRoute with PSR-7 Wrapper Library.
v4.0.0-beta2
2017-09-22 03:11 UTC
Requires
- php: >=7.0
- http-interop/http-middleware: ^0.4.1
- nikic/fast-route: ^1.2
- psr/container: ^1
- psr/http-message: ^1
Suggests
- wandu/di: for the Wandu class loader.
- wandu/http: for the Wandu responsifier.
- dev-master / 4.0.x-dev
- v4.0.0-beta2
- v4.0.0-beta1
- 3.0.x-dev
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v3.0.0-beta6
- v3.0.0-beta5
- v3.0.0-beta4
- v3.0.0-beta3
- v3.0.0-beta2
- v3.0.0-beta1
- v3.0.0-alpha3
- v3.0.0-alpha2
- v3.0.0-alpha1
- 0.7.x-dev
- v0.7.0
- v0.6.1
- v0.6.0
- v0.5.3
- v0.5.2
- v0.5.1
- v0.5.0
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.0
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.2
- v0.1.1
- v0.1.0
- dev-develop
This package is not auto-updated.
Last update: 2021-04-13 21:29:04 UTC
README
FastRoute with PSR-7 Wrapper Library.
Installation
composer require wandu/router
Basic Usage
$dispatcher = new \Wandu\Router\Dispatcher(); $routes = $dispatcher->createRouteCollection(); $routes->get('/', HomeController::class); $routes->get('/users', UserController::class, 'index'); $routes->get('/users/:id', UserController::class, 'show'); $request = new ServerRequest('GET', '/'); // PSR7 ServerRequestInterface implementation $response = $dispatcher->dispatch($routes, $request); static::assertInstanceOf(ResponseInterface::class, $response); static::assertEquals('index', $response->getBody()->__toString()); $request = new ServerRequest('GET', '/nothing'); // PSR7 ServerRequestInterface implementation try { $dispatcher->dispatch($routes, $request); } catch (RouteNotFoundException $e) { static::assertEquals('Route not found.', $e->getMessage()); }
class HomeController { public static function index() { return new Response(200, new StringStream("index")); } }
Pattern Routes
$routes->get('/users/:id(\d+)?', UserController::class, 'show'); $routes->get('/users-:id', UserController::class, 'show');
class UserController { public static function show(ServerRequestInterface $request) { return new Response(200, new StringStream("{$request->getAttribute('id')}")); } }
You can use all patterns in path-to-regexp.