budgetdumpster/hal-formatter
Composer 安装命令:
composer require budgetdumpster/hal-formatter
包简介
HAL Output formatting
README 文档
README
Installation
composer require budgetdumpster/hal-formatter:dev-master
Tests
Tests can be run using the command phpunit in the library root directory
Usage
Configuration
If you don't have a resource that has embedded resources, the $embedded parameter in the HalFormatter::formatResource
is optional. However, if you do want to incorporate embedded resources into your response, the configuration will help you
build out the desired structure.
The library is based on the assumption that related resources, in most ORMs, end up in a property on the model when the relationship is retrieved. What the configuration allows you to do is move those related resources to the embedded section of the response based on the property.
So for example, you might have something like:
Model::person_id - as the identifier for the related resource
Model::person - the property the actual Person model is pulled into
and as a configuration you have:
$embedded = [ 'person' => [ 'property' => 'person', 'key' => 'person', 'uri' => 'person' ] ];
What this configuration tells us is - we are going to take the data in the Model::person property and move
it to the embedded resources under the $key key and any links for that resource will use the value of $uri.
The library will work with individual models as well as arrays or collections of models.
Formatting a resource or collection
The most difficult part of this library is understanding the configuration (and since it's a matrix, you can
tack on multiple configurations. Let's use the example configuration above and the assumption that our model
will have a property on it called person
<?php
use BudgetDumpster\Formatters\HalFormatter;
use NameSpace\Models\Address;
$address = new Address();
$collection = $address->where('city', 'like', '%Westlake%');
$embedded = [
'person' => [
'property' => 'person',
'key' => 'person',
'uri' => 'person'
]
];
$formatter = new HalFormatter();
$responseBody = $formatter->formatResource($collection, $uri = 'address', $embedded);
This will return an address collection, each entry in the address collection will have the person property removed and will have a person
collection in the embedded resources.
In addition to the small API available, HalFormatter extends Nocarrier\Hal, so all the native functions of Nocarrier\Hal are available for use
budgetdumpster/hal-formatter 适用场景与选型建议
budgetdumpster/hal-formatter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.87k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 11 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「api」 「hal」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 budgetdumpster/hal-formatter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 budgetdumpster/hal-formatter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 budgetdumpster/hal-formatter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PHP implementation of HAL http://stateless.co/hal_specification.html
Kinikit - PHP Application development framework MVC component
Build a Hypermedia API response once and return it in multiple formats
ext-json wrapper with sane defaults
HAL+JSON & HAL+XML API transformer outputting valid API responses.
A package to cast json fields, each sub-keys is castable
统计信息
- 总下载量: 2.87k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-28