tbolner/flex-php-io
Composer 安装命令:
composer require tbolner/flex-php-io
包简介
IO library for web and CLI applications.
README 文档
README
An example of an I/O library. This project shows an alternative to handling output through the router framework. Most router frameworks expect the output to be passed back in the controllers in a "return" statement. Then for example they would convert the array to JSON.
In my opinion that solution becomes too problematic, when changes and new output modes are introduced later on to the project. Routing and IO are better be separated into 2 distinct libraries.
Functions:
- Help handling output buffering
- Handy methods to change content type
- Access HTTP/GET parameters in a type-safe manner.
- Help with the JSON format (input / output)
| Request | Response |
|---|---|
| Request::getPostedJson(): array | Response::printJson(array $json, $http_status_code = 200) |
| Request::getIntParameter(string $paramName): int | Response::bufferStart() |
| Request::getFloatParameter(string $paramName): float | Response::bufferEndClean() |
| Request::getStringParameter(string $paramName): string | Response::isBufferEnabled(): bool |
| Request::getBoolParameter(string $paramName): bool | Response::JsonContentType($http_status_code = 200) |
| Request::isParameterSet(string $paramName): bool | Response::HtmlContentType($http_status_code = 200) |
| Request::getHttpRequestMethod(): string | Response::TextContentType($http_status_code = 200) |
For everything else, use the built-in functions of PHP.
Recommended IDE: PhpStorm
Packagist page: https://packagist.org/packages/tbolner/flex-php-io
Example usages
<?php declare(strict_types=1); namespace MyProject; use FlexPhpIO\Response; Response::bufferStart(); Response::HtmlContentType(); echo '<html><body><p>Hello World!</p></body></html>';
<?php declare(strict_types=1); namespace MyProject; use FlexPhpIO\Request; use FlexPhpIO\Response; try { Response::bufferStart(); $input_data = Request::getPostedJson(); $message = @trim((string)$input_data["message"]); if ($message == "") { throw new \Exception("Missing or invalid 'message' field in input."); } Response::printJson([ "status" => "ok", "message" => $message ]); } catch (\Exception $ex) { Response::printJson([ "status" => "error", "message" => $ex->getMessage(); ], 400); }
An example for using it in a controller of FlexPhpRouter:
- project1/src/Controller/items.php
<?php declare(strict_types=1); namespace MyProject\Controller; use FlexPhpRouter\Router; use FlexPhpIO\Response; Router::get( "items/{id:int}/info", function (int $id) { Response::printJson([ "item_id" => $id ]); } );
Notes:
- When the output buffering is enabled, then you can change the headers any time, even after you have printed some output.
- When the output buffering is disable, then you cannot change the headers anymore after data was written to the output.
- In the FlexPhpRouter example, the output buffering should've already been set up in the main initialization file or entry point.
Installation
{
"require": {
"tbolner/flex-php-io": "dev-master"
}
}
Then execute:
composer update
This library requires PHP 7.1 or newer.
tbolner/flex-php-io 适用场景与选型建议
tbolner/flex-php-io 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.69k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 05 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「cli」 「web」 「io」 「content-type」 「output-buffering」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tbolner/flex-php-io 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tbolner/flex-php-io 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tbolner/flex-php-io 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Model of a web-based resource
Retrieve a WebResourceInterface instance over HTTP
Selectize Theme for Bootstrap 4
Provides command to manage a web library directory
Slim starter / Slim skeleton package to boost your development with Slim framework
Generates a trait to help ease the access of custom repo methods
统计信息
- 总下载量: 8.69k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2018-05-05