snicco/content-negotiation-middleware

v2.0.0-beta.9 2024-09-07 14:27 UTC

README

codecov Psalm Type-Coverage Psalm level PhpMetrics - Static Analysis PHP-Versions

一个使用 middlewares/negotiation 的中间件,并设置了一些合理的默认值。

它将根据提供的 Accept 头部确定最佳的内容类型和内容语言,并将相应地更新请求的 Accept 头部,使其只包含一个内容类型/内容语言。

如果无法确定匹配的内容类型,将抛出 406 HTTPException

安装

composer require snicco/content-negotiation-middleware

用法

这个中间件应该在全局的 MiddlewareResolver 中添加。

它必须在 snicco/http-routing 组件使用的 PSR-11 容器中绑定。

// In your container definitions
use Snicco\Middleware\Negotiation\NegotiateContent;

// basic configuration based on defaults.
$negotiation = new NegotiateContent(
   ['en'] // content languages
);

// With custom configuration for content-types your application can provide (sorted by priority)
$negotiation = new NegotiateContent(
   ['en'],
   [ 
       'html' => [
                'extension' => ['html', 'php'],
                'mime-type' => ['text/html'],
                'charset' => true,
            ],
       'txt' => [
                'extension' => ['txt'],
                'mime-type' => ['text/plain'],
                'charset' => true,
            ],
       'json' => [
                'extension' => ['json'],
                'mime-type' => ['application/json'],
                'charset' => true,
            ],
   ] 
);

贡献

此存储库是 Snicco 项目 开发仓库的只读分支。

以下是您可以贡献的方式.

报告问题和发送拉取请求

请在此 Snicco monorepo 中报告问题。

安全

如果您发现安全漏洞,请按照我们的 披露程序 操作。