middlewares/request-handler
Composer 安装命令:
composer require middlewares/request-handler
包简介
Middleware to execute request handlers
README 文档
README
Middleware to execute request handlers discovered by a router.
Requirements
- PHP >= 7.2
- A PSR-7 http library
- A PSR-15 middleware dispatcher
- Optionally, a PSR-11 container to resolve the route handlers
Installation
This package is installable and autoloadable via Composer as middlewares/request-handler.
composer require middlewares/request-handler
You may also want to install any route middleware like middlewares/fast-route or middlewares/aura-router for routing.
Purpose
There are two completely separate steps when it comes to route handling:
- Determining if the request is valid and can be resolved by the application.
- Handling the request inside the application.
The first step usually resolves into a route callback, while the product of the second one is usually the result of executing that callback.
Multiple things that can happen between the first and second steps: input validation, authentication, authorization, etc. and in some scenarios we may not want to continue processing the request (e.g. auth, accessing DB resources, etc.) if that would ultimately fail to resolve e.g. procuding an HTTP 400 error.
Splitting routing from request handling allows us to use any middleware between these two steps. It also makes the request-handler middleware able to be used with any routing component.
Example
A routing middleware needs to be called before the request can be handled. In this example, we will use fast-route middleware.
// Create the routing dispatcher $fastRouteDispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) { $r->get('/hello/{name}', HelloWorldController::class); }); $dispatcher = new Dispatcher([ new Middlewares\FastRoute($fastRouteDispatcher), // ... new Middlewares\RequestHandler(), ]); $response = $dispatcher->dispatch(new ServerRequest('/hello/world'));
When the request handler is invoked, it expects a request attribute to be defined that contains a reference to the handler. The handler must be a string, a callable or an object implementing MiddlewareInterface or RequestHandlerInterface. If it's a string, a ContainerInterface will be used to resolve it and get the MiddlewareInterface or RequestHandlerInterface to use. If it's a callable, it will be converted automatically to MiddlewareInterface using the Middlewares\Utils\CallableHandler
// Use a PSR-11 container to create the instances of the request handlers $container = new RequestHandlerContainer(); $dispatcher = new Dispatcher([ // ... new Middlewares\RequestHandler($container), ]);
Usage
Define the container used to resolve the handlers if they are provided as string (or an array with 2 strings). By default will use Middlewares\Utils\RequestHandlerContainer.
// Use the default PSR-11 container to create the intances of the request handlers $handler = new Middlewares\RequestHandler(); // Use a custom PSR-11 container $container = new RequestHandlerContainer(); $handler = new Middlewares\RequestHandler($container);
handlerAttribute
Configures the attribute name used to get the handler reference in the server request. The default is request-handler.
Dispatcher::run([ (new Middlewares\RequestHandler())->handlerAttribute('route'), ]);
continueOnEmpty
If the server request attribute is empty or does not exists, an exception is throwed. This function changes this behavior to continue with the next middleware.
Dispatcher::run([ //Try this, and if it's empty, continue (new Middlewares\RequestHandler())->continueOnEmpty(), //So we can try that (new Middlewares\RequestHandler())->handlerAttribute('other'), ]);
Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.
The MIT License (MIT). Please see LICENSE for more information.
middlewares/request-handler 适用场景与选型建议
middlewares/request-handler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.74M 次下载、GitHub Stars 达 45, 最近一次更新时间为 2017 年 04 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「invoke」 「controller」 「server」 「middleware」 「request」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 middlewares/request-handler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 middlewares/request-handler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 middlewares/request-handler 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Give your JS App some Backbone with Models, Views, Collections, and Events.
repository php library
Utility classes for analyse code and invoking callables with verification of arguments and type casting
Swoole synchronous blocking code invoke library
free as in freedom php component to easy up usage (reading and writing) of csv files for php 5.6 and above
Create mail from controller action render
统计信息
- 总下载量: 1.74M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 47
- 点击次数: 24
- 依赖项目数: 26
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-04-19