pwm/sfw-router
最新稳定版本:1.1.0
Composer 安装命令:
composer require pwm/sfw-router
包简介
A simple Router that maps incoming requests to predefined handlers
README 文档
README
A simple Router that maps incoming requests to predefined handlers.
It builds a tree using segments of a predefined uri as internal nodes and its corresponding handler as the terminal node. It resolves routes by traversing this tree.
Wildcard segments are supported and captured for use in the handler. See usage for more detail.
Exact segment match wins over wildcard match. Eg. if you have /foo/bar and /foo/{x} defined as routes with corresponding handlers Bar and X then /foo/bar will be handled by Bar while /foo/baz will be handled by X.
Table of Contents
Requirements
PHP 7.1+
Installation
composer require pwm/sfw-router
Usage
// Router depends on Request use SFW\Request\Request; use SFW\Request\RequestMethod as Method; use SFW\Request\RequestUri as Uri; // Have some controllers class FooCtrl { public function getAll(Request $request): array { /* ... */ } public function post(Request $request): bool { /* ... */ } } class BarCtrl { public function getById(Request $request, $fooId, $barId): Bar { /* ... */ } } // Create router $router = new Router(); // Add routes and corresponding route handlers $router->add(new Route(new Method(Method::GET), new Uri('/foo')), new RouteHandler(FooCtrl::class, 'getAll')); $router->add(new Route(new Method(Method::POST), new Uri('/foo')), new RouteHandler(FooCtrl::class, 'post')); $router->add(new Route(new Method(Method::GET), new Uri('/foo/{id}/bar/{id}')), new RouteHandler(BarCtrl::class, 'getById')); // Resolve a handler for an incoming request $routeHandler = $router->resolve(new Route($request->getMethod(), $request->getUri())); // (Optional) Resolve the handler class from the container and call the handling method $response = $container ->resolve($routeHandler->getClassName()) ->{$routeHandler->getMethodName()}($request, ...$routeHandler->getRoute()->getCapturedSegments());
How it works
TBD
Tests
$ vendor/bin/phpunit
$ composer phpcs
$ composer phpstan
Changelog
Licence
统计信息
- 总下载量: 6.9k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-04-27