f3-factory/fatfree-psr7-factory
Composer 安装命令:
composer require f3-factory/fatfree-psr7-factory
包简介
PSR-7/17 factory adapter for the PHP fat-free framework
README 文档
README
This is a plugin to enable PSR-7 HTTP message usage by the framework core.
In order to do so, this package can register any PSR-17 compatible HTTP message factory to the frameworks' core dependency injection container, that can be used to create and hydrate a Request (or ServerRequest) object from current framework globals, so it can be consumed by the route-handling controller or any other package.
Installation
This plugin requires:
- at least
fatfree-coreversion 4.0 - the DI
CONTAINERenabled - an actual HTTP message implementation package to be installed, i.e.
f3-factory/fatfree-psr7- or
nyholm/psr7 - or any other PSR7 package.
- a PSR17 HTTP message factory implementation (already included in
f3-factory/fatfree-psr7)
To install this package run:
composer require f3-factory/fatfree-psr7-factory
Register factory service
The HTTP message objects (PSR-7) are created by a related PSR-17 factory.
At first, we need to register the PSR-17 factories for every PSR-7 interface. Some PSR-17 packages have different factories for each interface, some share the same for all of them.
This example uses the Psr17Factory from our own fatfree-psr7 package, which is very fast and of course fat-free.
To install it run composer require f3-factory/fatfree-psr7 OR install any other PSR-7 and PSR-17 package you'd like to use instead.
In your bootstrap code or front controller (i.e. index.php):
// create the PSR17 adapter $psrAdapter = F3\Http\MessageFactory::instance(); // register the factories: $psrAdapter->register( requestFactory: Psr17Factory::class, responseFactory: Psr17Factory::class, serverRequestFactory: Psr17Factory::class, uploadedFileFactory: Psr17Factory::class, uriFactory: Psr17Factory::class, streamFactory: Psr17Factory::class, ); // register the concrete Request / Response objects $psrAdapter->registerRequest(RequestInterface::class); $psrAdapter->registerResponse(ResponseInterface::class); $psrAdapter->registerServerRequest(ServerRequestInterface::class);
NB: Instead of the code above, you can use this included shortcut, which will execute the same sequence for you, but it only works with our own fatfree-psr7 package:
MessageFactory::registerDefaults()
Registering the RequestInterface, ServerRequestInterface and ResponseInterface bindings will tell the dependency injection container which shortcut to use to resolve and hydrate the objects accordingly when you type-hinting them in your route controller.
This is essential to make the created request objects use the provided make methods by this factory in order to hydrate the objects with the actual data prepared by the framework. Requests use makeRequest/makeServerRequest and are filled with the currently available data from the F3 hive and SERVER globals. The Response factory will check for an existing response object in the RESPONSE hive variable and uses that instead to ensure you can pass the response between middlewares, route handlers or any other runtime services via dependency injection. You should return this object in route/middleware handlers, so the framework can care about the rest.
Usage
You can receive the Request and Response objects from any point now, i.e. from route handlers via auto-injection:
$f3->route('GET /hallo-world', function( Base $f3, RequestInterface $request, ResponseInterface $response, StreamFactoryInterface $streamFactory ) { $agent = $request->getHeaderLine('User-Agent'); return $response->withBody($streamFactory ->createStream('Your user agent is: '.$agent)); });
Alternatively you can create a new request object anywhere manually. These are going to be hydrated from the currently available $_SERVER and hive data via:
$request = $f3->make(RequestInterface::class); // or $request = MessageFactory::instance()->makeRequest(); // for Server Request: $serverRequest = $f3->make(ServerRequestInterface::class); // or $serverRequest = MessageFactory::instance()->makeServerRequest();
Response and the other classes should be instantiated via the PSR17 factory directly and do not need any special treatment by the framework core.
Move uploaded files
This is an example how to use the ServerRequest object to move an uploaded file.
$f3->route('POST /upload', function(Base $app, ServerRequestInterface $request) { $dir = './uploads/'; // handle uploaded files $files = $request->getUploadedFiles(); foreach ($files as $fieldName => $file) { if ($file instanceof UploadedFile) { $file->moveTo($dir.$file->getClientFilename()); } } };
For more usage examples see original PSR Http message readme:
f3-factory/fatfree-psr7-factory 适用场景与选型建议
f3-factory/fatfree-psr7-factory 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 11 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「F3」 「psr-7」 「http-message」 「fatfree」 「psr-17」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 f3-factory/fatfree-psr7-factory 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 f3-factory/fatfree-psr7-factory 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 f3-factory/fatfree-psr7-factory 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Model of a web-based resource
A High Performance API Framework By Swoole Extension
Some Excel and CSV utilities for PHP Fat-Free Framework
Interfaces for web resource models and services to retrieve and create them
Lightweight and strict implementation of PSR-7 HTTP Message including PSR-17 HTTP Factories.
Interface for a factory to create Psr\Http\Message\StreamInterface objects
统计信息
- 总下载量: 15
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 13
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-11-13