middlewares/negotiation 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

middlewares/negotiation

Composer 安装命令:

composer require middlewares/negotiation

包简介

Middleware to implement content negotiation

README 文档

README

Latest Version on Packagist Software License Testing Total Downloads

Middleware using wildurand/Negotiation to implement content negotiation. Contains the following components:

Requirements

Installation

This package is installable and autoloadable via Composer as middlewares/negotiation.

composer require middlewares/negotiation

Example

Dispatcher::run([
    new Middlewares\ContentType(),
    new Middlewares\ContentLanguage(['en', 'gl', 'es']),
    new Middlewares\ContentEncoding(['gzip', 'deflate']),
]);

ContentType

To detect the preferred mime type using the Accept header and the file extension and edit the header with this value. A Content-Type header is also added to the response if it's missing.

Define the formats to negotiate sorted by priority in the first argument. By default uses these

//Use the default types
$negotiator = new Middlewares\ContentType();

//Use only few types
$negotiator = new Middlewares\ContentType(['html', 'json']);

//Use only few types and configure some of them
$negotiator = new Middlewares\ContentType([
    'html',
    'json',
    'txt' => [
        'extension' => ['txt'],
        'mime-type' => ['text/plain'],
        'charset' => true,
    ]
]);

errorResponse

If no format matches the negotiation, by default the middleware use the first value in the list of available formats (by default text/html). Use this option to return a 406 error. Optionally, you can provide a Psr\Http\Message\ResponseFactoryInterface that will be used to create the response. If it's not defined, Middleware\Utils\Factory will be used to detect it automatically.

$responseFactory = new MyOwnResponseFactory();

//Use default html format (the first provided) if no valid format was detected (By default)
$negotiator = new Middlewares\ContentType(['html', 'json']);

//Return a 406 response if no valid format was detected
$negotiator = (new Middlewares\ContentType(['html', 'json']))->errorResponse();

//Return a 406 response using a specific responseFactory if no valid format was detected
$negotiator = (new Middlewares\ContentType(['html', 'json']))->errorResponse($responseFactory);

charsets

The available charsets to negotiate with the Accept-Charset header. By default is UTF-8.

$negotiator = (new Middlewares\ContentType())->charsets(['UTF-8', 'ISO-8859-1']);

noSniff

Adds the X-Content-Type-Options: nosniff header, to mitigating MIME confusión attacks.. Enabled by default.

//Disable noSniff header
$negotiator = (new Middlewares\ContentType())->noSniff(false);

attribute

To store the format name (json, html, css etc) in an attribute of the ServerRequest.

ContentLanguage

To detect the preferred language using the Accept-Language header or the path prefix and edit the header with this value. A Content-Language header is also added to the response if it's missing.

The first argument is an array with the available languages to negotiate sorted by priority. The first value will be used as default if no other languages is choosen in the negotiation.

$request = Factory::createServerRequest('GET', '/')
    ->withHeader('Accept-Language', 'gl-es, es;q=0.8, en;q=0.7');

Dispatcher::run([
    new Middlewares\ContentLanguage(['es', 'en']),

    function ($request) {
        $language = $request->getHeaderLine('Accept-Language');

        switch ($language) {
            case 'es':
                return 'Hola mundo';
            case 'en':
                return 'Hello world';
        }
    }
], $request);

usePath

By enabling this option, the base path will be used to detect the language. This is useful if you have different paths for each language, for example /gl/foo and /en/foo.

Note: the language in the path has preference over the Accept-Language header.

$request = Factory::createServerRequest('GET', '/en/hello-world');

Dispatcher::run([
    (new Middlewares\ContentLanguage(['es', 'en']))->usePath(),

    function ($request) {
        $language = $request->getHeaderLine('Accept-Language');

        switch ($language) {
            case 'es':
                return 'Hola mundo';
            case 'en':
                return 'Hello world';
        }
    }
], $request);

redirect

Used to return a 302 responses redirecting to the path containing the language. This only works if usePath is enabled, so for example, if the request uri is /welcome, returns a redirection to /en/welcome.

$responseFactory = new MyOwnResponseFactory();

//Use not only the Accept-Language header but also the path prefix to detect the language
$negotiator = (new Middlewares\ContentLanguage(['es', 'en']))->usePath();

//Returns a redirection with the language in the path if it's missing
$negotiator = (new Middlewares\ContentLanguage(['es', 'en']))->usePath()->redirect();

//Returns a redirection using a specific response factory
$negotiator = (new Middlewares\ContentLanguage(['es', 'en']))->usePath()->redirect($responseFactory);

ContentEncoding

To detect the preferred encoding type using the Accept-Encoding header and edit the header with this value.

$request = Factory::createServerRequest('GET', '/')
    ->withHeader('Accept-Encoding', 'gzip,deflate');

Dispatcher::run([
    new Middlewares\ContentEncoding(['gzip']),

    function ($request) {
        echo $request->getHeaderLine('Accept-Encoding'); //gzip
    }
], $request);

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/negotiation 适用场景与选型建议

middlewares/negotiation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 457.39k 次下载、GitHub Stars 达 46, 最近一次更新时间为 2016 年 10 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「http」 「language」 「server」 「middleware」 「encoding」 「content」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 middlewares/negotiation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 middlewares/negotiation 我们能提供哪些服务?
定制开发 / 二次开发

基于 middlewares/negotiation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 457.39k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 46
  • 点击次数: 33
  • 依赖项目数: 14
  • 推荐数: 2

GitHub 信息

  • Stars: 46
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-10-01