czepter/cakephp-fractal-transformer-view 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

czepter/cakephp-fractal-transformer-view

最新稳定版本:2.2.1

Composer 安装命令:

composer require czepter/cakephp-fractal-transformer-view

包简介

CakePHP view builder utilizing Fractal library for entities transformation

README 文档

README

Build Status Coverage Status License

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 4.0+ (use ~1.0 for CakePHP 3.1+)

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());

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.

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-09-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固