andrej-griniuk/cakephp-fractal-transformer-view
Composer 安装命令:
composer require andrej-griniuk/cakephp-fractal-transformer-view
包简介
CakePHP view builder utilizing Fractal library for entities transformation
README 文档
README
FractalTransformerView plugin for CakePHP
This plugin is a thin wrapper for JsonView that allows using Fractal transformers for your API output. What is Fractal?
Fractal provides a presentation and transformation layer for complex data output, the like found in RESTful APIs, and works really well with JSON. Think of this as a view layer for your JSON/YAML/etc. When building an API it is common for people to just grab stuff from the database and pass it to json_encode(). This might be passable for “trivial” APIs but if they are in use by the public, or used by mobile applications then this will quickly lead to inconsistent output.
Requirements
- CakePHP 5.x (use ~1.0 for CakePHP 3.x, ~2.0 for CakePHP 4.x)
Installation
You can install this plugin into your CakePHP application using Composer.
composer require andrej-griniuk/cakephp-fractal-transformer-view
Usage
To enable the plugin set FractalTransformerView.FractalTransformer class name for viewBuilder. Then you just do what you would normally do in your data views - specify which view vars you want to get serialized by setting serialize view builder option. E.g.:
namespace App\Controller; class ArticlesController extends AppController { public function initialize(): void { parent::initialize(); $this->loadComponent('RequestHandler'); $this->viewBuilder()->setClassName('FractalTransformerView.FractalTransformer'); } public function index() { // Set the view vars that have to be serialized. $this->set('articles', $this->paginate()); // Specify which view vars JsonView should serialize. $this->viewBuilder()->setOption('serialize', ['articles']); } }
The view will look for transformer class starting with entity name. E.g.:
namespace App\Model\Transformer; use App\Model\Entity\Article; use League\Fractal\TransformerAbstract; class ArticleTransformer extends TransformerAbstract { /** * Creates a response item for each instance * * @param Article $article post entity * @return array transformed post */ public function transform(Article $article) { return [ 'title' => $article->get('title') ]; } }
If transformer class not found the variable is serialized the normal way.
Custom transformer class name can be set by defining transformer view builder option:
$this->viewBuilder()->setOption('transform', ['articles' => '\App\Model\Transformer\CustomArticleTransformer']);
You can also define if you don't want to use transformer for certain variables:
$this->viewBuilder()->setOption('transform', ['articles' => false]);
You can set a custom serializer (class name or object) via serializer view builder option:
$this->viewBuilder()->setOption('serializer', new CustomSerializer());
Baking Transformers
To bake transformers you must include the plugin in your src/Application.php file. Add the following to your bootstrap method:
$this->addPlugin('FractalTransformerView');
You must also have the cakephp/bake composer package installed.
You can now run bin/cake bake transformer YOUR_MODEL to create transformers.
Bugs & Feedback
https://github.com/andrej-griniuk/cakephp-fractal-transformer-view/issues
Credits
Inspired by @josegonzalez Using Fractal to transform entities for custom api endpoints.
License
Copyright (c) 2016, Andrej Griniuk and licensed under The MIT License.
andrej-griniuk/cakephp-fractal-transformer-view 适用场景与选型建议
andrej-griniuk/cakephp-fractal-transformer-view 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 91.96k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2016 年 02 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「rest」 「api」 「cakephp」 「fractal」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 andrej-griniuk/cakephp-fractal-transformer-view 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 andrej-griniuk/cakephp-fractal-transformer-view 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 andrej-griniuk/cakephp-fractal-transformer-view 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
统计信息
- 总下载量: 91.96k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 18
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-02-08