bitexpert/pathfinder
最新稳定版本:v0.5.0
Composer 安装命令:
composer require bitexpert/pathfinder
包简介
PSR-7 compatible routing component
README 文档
README
A PHP routing component.
Router
The router is responsible for resolving the target from the given route as well building an uri for a given route identifier (and it`s parameters). Using the Psr7Router is pretty easy:
$router = new \bitExpert\Pathfinder\Psr7Router(); $router->setRoutes( [ new Route(['GET'], '/', 'index'), new Route(['GET'], '/question/[:title]_[:id]', 'question'), new Route(['GET', 'POST'], '/editquestion', 'editquestion') ] );
Routes
Routes in Pathfinder are implemented immutual. You may define your routes by creating a route instance directly as shown above or using the RouteBuilder for convenience (recommended):
use bitExpert\Pathfinder\RouteBuilder; use bitExpert\Pathfinder\Matcher\NumericMatcher; RouteBuilder::route() ->get('/') ->to('home') ->build(); RouteBuilder::route() ->from('/user') ->accepting('POST') ->accepting('PUT') ->to('userAction') ->build(); RouteBuilder::route() ->get('/') ->to(function () { }) ->named('home') ->build(); RouteBuilder::route() ->get('/user/[:userId]') ->to('userAction') ->ifMatches('userId', new NumericMatcher()) ->build();
Customizing the RouteBuilder
In case you need some special route classes for your application, you may configure the RouteBuilder to use your custom route class instead of Route either for a particular route:
$route = RouteBuilder::route(MyCustomRoute::class) ->get('/') ->to('home') ->build(); $route->doCustomStuffDefinedInYourClass();
or globally as default class to use:
RouteBuilder::setDefaultRouteClass(MyCustomRoute::class); $route = RouteBuilder::route() ->get('/') ->to('home') ->build(); $route->doCustomStuffDefinedInYourClass();
Matchers
Matchers are used to ensure that your route params match given criteria such as digits only:
RouteBuilder::route() ->get('/user/[:userId]') ->to('userAction') ->ifMatches('userId', new NumericMatcher()) ->build();
You may add several matchers for one param, just by adding them via:
RouteBuilder::route() ->get('/user/[:userId]') ->to('userAction') ->ifMatches('userId', new NumericMatcher()) ->ifMatches('userId', new MyCustomMatcher()) ->ifMatches('userId', function ($value) { }) ->build();
Middleware
You may use the BasicRoutingMiddleware to integrate Pathfinder into your PSR-7 compatible project:
use bitExpert\Pathfinder\Route; use bitExpert\Pathfinder\Psr7Router; use bitExpert\Pathfinder\Middleware\BasicRoutingMiddleware; $router = new Psr7Router(); $router->addRoute(RouteBuilder::route()->get('/')->to('home')->build()); // The routing middleware will use the given router to match the request and will set the routing result as value // of the request attribute named 'routingResult' for further use $routingMiddleware = new BasicRoutingMiddleware($router, 'routingResult');
Errors
The Psr7Router does not throw an Exception if the request doesn't match any route. It will still return a RoutingResult returning true when calling $routingResult->failed(); You may receive the failure reason by calling $routingResult->getFailure() which will give you information about the failure reason and an optional route which could have matched, but didn't fulfill all criteria:
use bitExpert\Pathfinder\Psr7Router; use bitExpert\Pathfinder\RouteBuilder; use Zend\Diactoros\ServerRequest; use bitExpert\Pathfinder\Matcher\NumericMatcher; $router = new Psr7Router(); $homeRoute = RouteBuilder::route() ->get('/') ->to('home') ->build(); $orderUpdateRoute = RouteBuilder::route() ->put('/order/[:orderId]') ->to('updateOrder') ->ifMatches('id', new NumericMatcher()) ->build(); $router->setRoutes([$homeRoute, $orderUpdateRoute]); // Not found example $request = new ServerRequest([], [], '/users', 'GET'); $result = $router->match($request); $result->failed(); // -> true $result->getFailure(); // -> RoutingResult::FAILED_NOT_FOUND $result->hasRoute(); // -> false // Method not allowed example $request = new ServerRequest([], [], '/order/1', 'GET'); $result = $router->match($request); $result->failed(); // -> true $result->getFailure(); // -> RoutingResult::FAILED_METHOD_NOT_ALLOWED $result->hasRoute(); // -> true $result->getRoute(); // -> $orderUpdateRoute // BadRequest example $request = new ServerRequest([], [], '/order/abc', 'PUT'); $result = $router->match($request); $result->failed(); // -> true $result->getFailure(); // -> RoutingResult::FAILED_BAD_REQUEST $result->hasRoute(); // -> true $result->getRoute(); // -> $orderUpdateRoute
License
Pathfinder is released under the Apache 2.0 license.
bitexpert/pathfinder 适用场景与选型建议
bitexpert/pathfinder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.23k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2015 年 11 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bitexpert/pathfinder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bitexpert/pathfinder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.23k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 12
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2015-11-29