phapi/middleware-content-negotiation

此包已废弃,不再维护。未建议替代包。

Phapi 框架使用的 Content Negotiation 中间件。

1.0.1 2015-09-02 09:08 UTC

This package is not auto-updated.

Last update: 2021-02-05 22:43:58 UTC


README

Build status Code Climate Test Coverage

内容协商中间件包含一个用于处理格式协商的中间件。它解析 Accept 头,将其与支持的字节类型列表(由序列化器注册)进行匹配,并在响应对象上设置正确的 Content-Type 头。

安装

默认情况下,此中间件包含在 Phapi 框架 中,但如果需要安装,它可通过 PackagistComposer 安装。

$ php composer.phar require phapi/middleware-content-negotiation:1.*

配置

中间件本身没有配置选项。

有关如何配置与 Phapi 框架集成的更多信息,请参阅配置文档

使用

格式协商中间件在响应对象上设置正确的 Content-Type 头。可以通过使用 getHeaderLine() 方法访问头值。

<?php
/*
 * Get the response content type header that has the negotiated header.
 *
 * Returns the header value including charset.
 * Example: application/json;charset=utf-8
 */
$mimeType = $response->getHeaderLine('Content-Type');

中间件还将 MIME 类型以及包含在 accept 头中的任何参数作为请求对象的属性设置。

<?php
// Get the negotiated mime type:
$mimeType = $request->getAttribute('Accept');

// Get parameters (as an array) included in the accept header
$acceptParameters = $request->getAttribute('Accept-Parameters');

异常

如果请求的 MIME 类型不受支持,中间件将抛出 406 NotAcceptable。如果没有找到序列化器,将抛出 500 InternalServerError

如果请求的 MIME 类型不受支持,将使用第一个注册的序列化器中的第一个 MIME 类型来序列化发送给客户端的错误消息。

Phapi

此中间件是 Phapi 包,由 Phapi 框架 使用。中间件也符合 PSR-7 并实现了 Phapi 中间件合约

许可

内容协商中间件遵循 MIT 许可证 - 有关详细信息,请参阅 license.md 文件。

贡献

欢迎贡献、修复错误等内容