ellipse/router-fastroute
Composer 安装命令:
composer require ellipse/router-fastroute
包简介
FastRoute Psr-15 middleware and request handler
README 文档
README
FastRoute Psr-15 middleware and request handler.
Require php >= 7.0
Installation composer require ellipse/router-fastroute
Run tests ./vendor/bin/kahlan
- Usage as request handler
- Usage as middleware
- Group count based middleware and request handler
- Dispatcher factories helpers
Usage as request handler
This package provides an Ellipse\Router\FastRouteRequestHandler Psr-15 request handler taking a fastroute dispatcher factory as parameter.
This factory can be any callable returning an implementation of FastRoute\Dispatcher and the route handlers it matches are expected to be implementations of Psr\Http\Server\RequestHandlerInterface.
When the FastRouteRequestHandler handles a request the Dispatcher produced by the factory is used to match a Psr-15 request handler. When the matched route pattern contains placeholders, a new request is created with those placeholders => matched value pairs as request attributes. Finally the matched request handler is proxied with this new request to actually return a response.
Using a factory allows to perform the time consuming task of mapping routes only when the request is handled with the FastRouteRequestHandler. If for some reason an application handles the incoming request with another request handler, no time is lost mapping routes for this one.
Regarding exceptions:
- An
Ellipse\Router\Exceptions\FastRouteDispatcherTypeExceptionis thrown when the factory does not return an implementation ofFastRoute\Dispatcher. - An
Ellipse\Router\Exceptions\MatchedHandlerTypeExceptionis thrown when the route handler matched by the fastroute dispatcher is not an implementation ofPsr\Http\Server\RequestHandlerInterface. - An
Ellipse\Router\Exceptions\NotFoundExceptionis thrown when no route match the url. - An
Ellipse\Router\Exceptions\MethodNotAllowedExceptionis thrown when a route matches the url but the request http method is not allowed by the matched route.
<?php namespace App; use FastRoute\RouteParser; use FastRoute\DataGenerator; use FastRoute\Dispatcher; use FastRoute\RouteCollector; use Ellipse\Router\FastRouteRequestHandler; // Get a psr7 request. $request = some_psr7_request_factory(); // Create a fastroute dispatcher factory. $factory = function ($r) { // Create a new fastroute route collector. $r = new RouteCollector( new RouteParser\Std, new DataGenerator\GroupCountBased ); // The route handlers must be Psr-15 request handlers. $r->get('/', new SomeRequestHandler); // When this route is matched a new request with an 'id' attribute would be passed to the request handler. $r->get('/path/{id}', new SomeOtherRequestHandler); // return a fastroute dispatcher return new Dispatcher\GroupCountBased($r->getData()); }; // Create a fastroute request handler using this factory. $handler = new FastRouteRequestHandler($factory); // When a route is matched the request is handled by this route request handler. // Otherwise NotFoundException or MethodNotAllowedException is thrown $response = $handler->handle($request);
Usage as middleware
This package provides an Ellipse\Router\FastRouteMiddleware Psr-15 middleware also taking a fastroute dispatcher factory as parameter.
Under the hood it creates a FastRouteRequestHandler with the given factory and use it to handle the request. When a NotFoundException is thrown, the request processing is delegated to the next middleware.
<?php namespace App; use FastRoute\RouteParser; use FastRoute\DataGenerator; use FastRoute\Dispatcher; use FastRoute\RouteCollector; use Ellipse\Router\FastRouteMiddleware; // Get a psr7 request. $request = some_psr7_request_factory(); // Create a fastroute dispatcher factory. $factory = function ($r) { // Create a new fastroute route collector. $r = new RouteCollector( new RouteParser\Std, new DataGenerator\GroupCountBased ); // The route handlers must be Psr-15 request handlers. $r->get('/', new SomeRequestHandler); // When this route is matched a new request with an 'id' attribute would be passed to the request handler. $r->get('/path/{id}', new SomeOtherRequestHandler); // return a fastroute dispatcher return new Dispatcher\GroupCountBased($r->getData()); }; // Create a fastroute middleware using this factory. $middleware = new FastRouteMiddleware($factory); // When a route is matched the request is handled by this route request handler. // When a NotFoundException is thrown, NextRequestHandler is used to handle the request. $response = $middleware->process($request, new NextRequestHandler);
Group count based middleware and request handler
As fastroute dispatchers are usually group count based, this package provides an Ellipse\Router\FastRoute\GroupCountBasedMiddleware and an Ellipse\Router\FastRoute\GroupCountBasedRequestHandler taking only a route definition callback as parameter allowing to easily create group count based fastroute middleware and request handler.
<?php namespace App; use Ellipse\Router\FastRoute\GroupCountBasedMiddleware; use Ellipse\Router\FastRoute\GroupCountBasedRequestHandler; // Create a route definition callback. $routeDefinitionCallback = function ($r) { // The route handlers must be Psr-15 request handlers. $r->get('/', new SomeRequestHandler); // When this route is matched a new request with an 'id' attribute would be passed to the request handler. $r->get('/path/{id}', new SomeOtherRequestHandler); }; // Create a fastroute middleware using a group count based dispatcher. $middleware = new GroupCountBasedMiddleware($routeDefinitionCallback); // Create a fastroute request handler using a group count based dispatcher. $handler = new GroupCountBasedRequestHandler($routeDefinitionCallback);
Dispatcher factories helpers
This package provides two dispatcher factories helpers proxying the fastroute ones: Ellipse\Router\FastRoute\SimpleDispatcher and Ellipse\Router\FastRoute\CachedDispatcher.
Those two classes are just callables proxying their respective fastroute functions to produce a dispatcher when the FastRouteRequestHandler handles the request.
<?php namespace App; use Ellipse\Router\FastRouteMiddleware; use Ellipse\Router\FastRouteRequestHandler; use Ellipse\Router\FastRoute\SimpleDispatcher; // Create a route definition callback like with fastroute simpleDispatcher function. $routeDefinitionCallback = function ($r) { // The route handlers must be Psr-15 request handlers. $r->get('/', new SomeRequestHandler); // When this route is matched a new request with an 'id' attribute would be passed to the request handler. $r->get('/path/{id}', new SomeOtherRequestHandler); }; // An optional array of options can be passed like with fastroute simpleDispatcher function. $options = [ // Can specify fastroute classes to use. ]; // Create a dispatcher factory using fastroute simpleDispatcher function. // Same with CachedDispatcher using fastroute cachedDispatcher. $factory = new SimpleDispatcher($routeDefinitionCallback, $options); // Create a fastroute middleware using this dispatcher factory. $middleware = new FastRouteMiddleware($factory); // Create a fastroute request handler using this dispatcher factory. $handler = new FastRouteRequestHandler($factory);
ellipse/router-fastroute 适用场景与选型建议
ellipse/router-fastroute 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 172 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 06 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「router」 「middleware」 「FastRoute」 「psr-15」 「request-handler」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ellipse/router-fastroute 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ellipse/router-fastroute 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ellipse/router-fastroute 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
FAstRoute integration for Objective PHP
Router
Slim Framework 3 CSRF protection middleware utilities
PMVC App Action Router
Cloudflare Middleware For Guzzle
统计信息
- 总下载量: 172
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-06-07