wandu/router
最新稳定版本:v4.0.0-beta2
Composer 安装命令:
composer require wandu/router
包简介
FastRoute with PSR-7 Wrapper Library.
README 文档
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.
Reference
统计信息
- 总下载量: 2.95k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-05-19