定制 cn-tools/negotiation-middleware 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

cn-tools/negotiation-middleware

Composer 安装命令:

composer require cn-tools/negotiation-middleware

包简介

Content negotiation middleware for Slim v4 / PSR-15.

README 文档

README


ℹ️ WARNING:

This repo is based on rszrama/negotiation-middleware.
I try/made a porting to slim v4 only and add some little test cases.

This repo is in DEV-Mode! PR are welcome!

The content of this README is not actuall! Be aware to check the code by yourself!

NegotiationMiddleware provides content negotiation middleware for PHP applications using a middleware signature that requires a request object, a response object, and the next callable in the middleware stack.

This library depends on willdurand/negotiation for content negotiation. It allows you to add negotiation to a middleware stack that:

  1. Identifies and matches against a list of acceptable media types.
  2. Supplies a default media type in the absence of an Accept request header.
  3. Enriches the request object with the negotiated media type.

If the negotiator cannot determine which media type to use in response to the request, it will return a response with a 406 Not Acceptable status.

Installation

Use Composer to install NegotiationMiddleware:

$ composer require cn-tools/negotiation-middleware

This will install the library and its dependencies. NegotiationMiddleware requires PHP 8.0 or newer.

Usage - POSSIBLY WRONG

Add an instance of NegotiationMiddleware\Negotiator to an application or route level middleware stack, passing two arguments to the constructor: an array of acceptable media types to be matched against and a boolean indicating whether or not the middleware should simply match the first acceptable media type in the absence of an Accept header in the request.

Example from Slim 4.x:

<?php

require 'vendor/autoload.php';

use \NegotiationMiddleware\Negotiator;

$app = new \Slim\App();

$app
    ->get('/hello', function(Slim\Http\Request $request, Slim\Http\Response $response, $args) {
        return $response->write('Hello, world!');
    })
    ->add(new Negotiator(['text/html', 'application/json'], TRUE));

$app->run();

In this case, a request to /hello from a web browser will more than likely match the text/html media type specified as an acceptable media type for this route (which is just as well, given our response isn't valid JSON). However, if the server received a request that did not have an Accept header, it will still match to text/html since we instructed the object to supply a default media type.

Note that the Slim\Http\Request object implements PSR-7's ServerRequestInterface. Negotiator::__invoke() requires an instance of this interface to store the negotiated media type in the request object using an attribute named mediaType.

Thus, the route closure could print the negotiated media type like so:

$app
    ->get('/hello', function(Slim\Http\Request $request, Slim\Http\Response $response, $args) {
        $mediaType = $request->getAttribute('mediaType')->getValue();
        return $response->write($mediaType);
    })
    ->add(new Negotiator(['text/html', 'application/json'], TRUE));

The mediaType attribute is an instance of \Negotiation\Accept, which contains a variety of methods for inspecting the actual matched media type from the request header. Refer to the class documentation of willdurand/negotiation for more information.

Middleware Signature - POSSIBLY WRONG

NegotiationMiddleware uses the middleware signature required by Slim 4.x, that of a callable that accepts two arguments: a PSR-7 request object, a PSR-15 response object, and a callable that represents the next middleware in the stack.

This pattern and its usefulness thanks to the adoption of PSR-7 are discussed in a helpful blog post by Matthew O'Phinney, including a similar example:

<?php

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

class Middleware {
    public function function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
      // Execute code before calling the next middleware.
      $response = $handler->handle($request);
      // Execute code after calling the next middleware.
      return $response;
    }
    ...
}

Since this implementation is not specific to Slim (or any other framework), it can be reused by any application that uses the same middleware signature or adapted to work with other middleware patterns that still make use of PSR-7 request and response objects.

License

NegotiationMiddleware is licensed under the MIT license. See LICENSE for more information.

cn-tools/negotiation-middleware 适用场景与选型建议

cn-tools/negotiation-middleware 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-09