yiisoft/data-response
Composer 安装命令:
composer require yiisoft/data-response
包简介
Allows responding with data that is automatically converted into PSR-7 response
README 文档
README
Yii Data Response
The package allows responding with data that is automatically converted into PSR-7 response.
Requirements
- PHP 8.1 - 8.5.
DOMPHP extension.
Installation
The package could be installed with Composer:
composer require yiisoft/data-response
General usage
Response Factories
The package provides response factories that create PSR-7 responses
with DataStream body. The data is formatted lazily when the response body is read.
use Yiisoft\DataResponse\ResponseFactory\JsonResponseFactory; use Yiisoft\DataResponse\Formatter\JsonFormatter; /** * @var Psr\Http\Message\ResponseFactoryInterface $responseFactory */ $factory = new JsonResponseFactory($responseFactory, new JsonFormatter()); $response = $factory->createResponse(['key' => 'value']); $response->getBody()->rewind(); echo $response->getBody()->getContents(); // {"key":"value"} echo $response->getHeaderLine('Content-Type'); // application/json; charset=UTF-8
The following response factories are available:
JsonResponseFactory— creates responses with JSON-formatted body;XmlResponseFactory— creates responses with XML-formatted body;HtmlResponseFactory— creates responses with HTML-formatted body;PlainTextResponseFactory— creates responses with plain text body;DataResponseFactory— creates responses without a predefined formatter, use middleware to format.
Middleware
The package provides PSR-15 middleware that formats DataStream responses
without a predefined formatter.
use Yiisoft\DataResponse\Middleware\JsonDataResponseMiddleware; use Yiisoft\DataResponse\Formatter\JsonFormatter; $middleware = new JsonDataResponseMiddleware(new JsonFormatter());
The following middleware are available:
HtmlDataResponseMiddlewareJsonDataResponseMiddlewareXmlDataResponseMiddlewarePlainTextDataResponseMiddleware
Content Negotiation
The package provides content negotiation via middleware and response factory.
Middleware
ContentNegotiatorDataResponseMiddleware selects a formatter based on the request's Accept header:
use Yiisoft\DataResponse\Formatter\HtmlFormatter; use Yiisoft\DataResponse\Formatter\XmlFormatter; use Yiisoft\DataResponse\Formatter\JsonFormatter; use Yiisoft\DataResponse\Middleware\ContentNegotiatorDataResponseMiddleware; $middleware = new ContentNegotiatorDataResponseMiddleware( formatters: [ 'text/html' => new HtmlFormatter(), 'application/xml' => new XmlFormatter(), 'application/json' => new JsonFormatter(), ], fallback: new JsonFormatter(), );
The fallback parameter also accepts a RequestHandlerInterface, for example NotAcceptableRequestHandler
to return a 406 response when no formatter matches.
Response Factory
ContentNegotiatorResponseFactory selects a response factory based on the request's Accept header:
use Yiisoft\DataResponse\ResponseFactory\ContentNegotiatorResponseFactory; use Yiisoft\DataResponse\ResponseFactory\JsonResponseFactory; use Yiisoft\DataResponse\ResponseFactory\XmlResponseFactory; /** * @var JsonResponseFactory $jsonResponseFactory * @var XmlResponseFactory $xmlResponseFactory */ $factory = new ContentNegotiatorResponseFactory( factories: [ 'application/json' => $jsonResponseFactory, 'application/xml' => $xmlResponseFactory, ], fallback: $jsonResponseFactory, ); $response = $factory->createResponse($request, ['key' => 'value']);
The fallback parameter also accepts a RequestHandlerInterface, for example NotAcceptableRequestHandler
to return a 406 response when no factory matches.
DataStream
DataStream is a PSR-7 stream that lazily formats data.
It wraps raw data and a formatter, and performs formatting only when the stream is read.
A formatter is optional at construction time, but must be set before reading the stream,
otherwise a LogicException will be thrown.
use Yiisoft\DataResponse\DataStream\DataStream; use Yiisoft\DataResponse\Formatter\JsonFormatter; use Yiisoft\DataResponse\Formatter\XmlFormatter; $stream = new DataStream(['key' => 'value'], new JsonFormatter()); echo (string) $stream; // {"key":"value"} // You can change the data or formatter dynamically $stream->changeData(['new' => 'data']); $stream->changeFormatter(new XmlFormatter());
Documentation
If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.
License
The Yii Data Response is free software. It is released under the terms of the BSD License.
Please see LICENSE for more information.
Maintained by Yii Software.
Support the project
Follow updates
yiisoft/data-response 适用场景与选型建议
yiisoft/data-response 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 313.78k 次下载、GitHub Stars 达 19, 最近一次更新时间为 2020 年 07 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「data」 「response」 「psr-7」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yiisoft/data-response 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yiisoft/data-response 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yiisoft/data-response 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Model of a web-based resource
Adds the EDTF data type to Wikibase
A simple library that allows transform any kind of data to native php data or whatever
Interfaces for web resource models and services to retrieve and create them
HTTP request logger middleware for Laravel
统计信息
- 总下载量: 313.78k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 19
- 点击次数: 18
- 依赖项目数: 38
- 推荐数: 2
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2020-07-15