gofabian/negotiation-middleware
Composer 安装命令:
composer require gofabian/negotiation-middleware
包简介
PHP negotiation middleware for accept headers of PSR-7 requests. Uses the negotiation library willdurand/negotiation.
README 文档
README
The Negotiation Middleware is a PHP library that negotiates accept headers of HTTP requests. The middleware chooses the most fitting options by looking at the accepted values of client and server. It supports the headers accept, accept-language, accept-encoding and accept-charset.
This library is a middleware for the Slim framework 3 but may be used for any PHP code that uses HTTP messages conform to PSR-7. The Negotiation Middleware is based on the library from William Durand.
Installation
The recommended way to install the Negotiation Middleware is using Composer:
$ composer require gofabian/negotiation-middleware
Composer fetches all dependencies automatically. The Negotiation Middleware uses common standards and tries to reduce the number of required packages follwing the KISS principle:
- PHP 7.3+
- PSR-7
- willdurand/negotiation 3.x
- recommended: slim/slim 3.x
Usage
The first example describes how to use the Slim framework in combination with the Negotiation Middleware. The following examples are less detailed and bring specific aspects into focus.
Slim and the Negotiation Middleware
The most common way is to negotiate the media type. In this example the server accepts the media types text/html and application/json:
<?php use Gofabian\Negotiation\NegotiationMiddleware; // create Slim app $app = new \Slim\App; // configure middleware $app->add(new NegotiationMiddleware([ 'accept' => ['text/html', 'application/json'] ])); // use negotiated media type $app->get('/mediatype', function ($request, $response, $args) { $negotiation = $request->getAttribute('negotiation'); $mediaType = $negotiation->getMediaType(); return $response->write("media type = " . $mediaType); }); // run app $app->run();
Let's have a look at incoming HTTP requests and the priority of accepted media types:
-
If the accept header includes
text/htmlthe HTTP response will bemedia type = text/html. The first entry of the configured media types has the highest priority. -
If the accept header includes
application/jsonbut nottext/htmlthe HTTP response will bemedia type = application/json. -
If the accept header is empty the HTTP response will be
media type = text/html. By default the entry with highest priority will be chosen if the accept header does not exist. -
If the accept header is available but includes neither
application/jsonnortext/htmlthe HTTP response will have status 406 "Not Acceptable".
Accept headers
In addition to media type the middleware negotiates language, charset and encoding:
$app->add(new NegotiationMiddleware([ 'accept' => ['text/html', 'application/json'], 'accept-language' => ['en', 'de-DE'], 'accept-encoding' => ['gzip'], 'accept-charset' => ['utf-8', 'ascii'] ]));
HTTP Status 406 - Not Acceptable
If the HTTP request contains an accept header but none of its values is accepted by the Negotiation Middleware the HTTP response will have the HTTP status 406 "Not Acceptable".
If the HTTP request does not contain an accept header the Negotiation Middleware will take the value with highest priority. Alternatively you can answer such requests with HTTP status 406, too:
$app->add(new NegotiationMiddleware( [ 'accept-language' => ['en', 'de-DE'] ], false // 406 status for empty accept headers ));
In this example the Negotiation Middleware will return HTTP status 406 if the HTTP request does not contain the accept header accept-language.
Negotiation result
The Negotiation Middleware puts the negotiation result into an instance of AcceptProvider and attaches it as an attribute to the PSR-7 HTTP request. The attribute name is negotiation by default and may be set like this:
$app->add(new NegotiationMiddleware( [ 'accept' => ['text/xml'] ], true, 'foo' // custom name )); $app->get('/example', function($request, $response, $args) { $result = $request->getAttribute('foo'); return $response->write($result->getMediaType()); });
The easy way to access the negotiation results is to use the attached AcceptProvider like this:
$result = $request->getAttribute('negotiation'); $result->getMediaType(); // these $result->getLanguage(); // getters $result->getCharset(); // return $result->getEncoding(); // strings
Alternatively you can get the original result objects of the negotiation library like this:
$result = $request->getAttribute('negotiation'); $result->getAccept(); // results $result->getAcceptLanguage(); // from $result->getAcceptCharset(); // negotiation $result->getAcceptEncoding(); // library
Have a look at the original documentation for more information.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
gofabian/negotiation-middleware 适用场景与选型建议
gofabian/negotiation-middleware 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 81.05k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2015 年 11 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「middleware」 「slim」 「negotiation」 「psr-7」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gofabian/negotiation-middleware 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gofabian/negotiation-middleware 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gofabian/negotiation-middleware 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Provides content-negotiation using Accept* headers for BEAR.Sunday
Secure session middleware for Slim 3 framework
A bundle that creates REST Apis based on your current controllers actions
Adds request-parameter validation to the SLIM 3.x PHP framework
Slim Framework 3 CSRF protection middleware utilities
统计信息
- 总下载量: 81.05k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 21
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-11-02