PHPackages                             andrej-griniuk/cakephp-fractal-transformer-view - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. andrej-griniuk/cakephp-fractal-transformer-view

ActiveCakephp-plugin[HTTP &amp; Networking](/categories/http)

andrej-griniuk/cakephp-fractal-transformer-view
===============================================

CakePHP view builder utilizing Fractal library for entities transformation

3.0(2y ago)1890.7k↓20.8%6MITPHPPHP &gt;=8.1

Since Feb 29Pushed 2y ago3 watchersCompare

[ Source](https://github.com/andrej-griniuk/cakephp-fractal-transformer-view)[ Packagist](https://packagist.org/packages/andrej-griniuk/cakephp-fractal-transformer-view)[ Docs](http://github.com/andrej-griniuk/cakephp-fractal-transformer-view)[ RSS](/packages/andrej-griniuk-cakephp-fractal-transformer-view/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (4)Versions (8)Used By (0)

[![Build Status](https://camo.githubusercontent.com/bef921f407f1adaede84d7b407133bac637edd5824780071aa704cef5fa63220/68747470733a2f2f6170702e7472617669732d63692e636f6d2f616e6472656a2d6772696e69756b2f63616b657068702d6672616374616c2d7472616e73666f726d65722d766965772e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/andrej-griniuk/cakephp-fractal-transformer-view)[![codecov](https://camo.githubusercontent.com/da0f3377c7436d4cd6f24ed52c159a85f63b579b2458e38a19ad0cd000b3eb1c/68747470733a2f2f636f6465636f762e696f2f67682f616e6472656a2d6772696e69756b2f63616b657068702d6672616374616c2d7472616e73666f726d65722d766965772f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/andrej-griniuk/cakephp-fractal-transformer-view)[![License](https://camo.githubusercontent.com/942e017bf0672002dd32a857c95d66f28c5900ab541838c6c664442516309c8a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265)](LICENSE)

FractalTransformerView plugin for CakePHP
=========================================

[](#fractaltransformerview-plugin-for-cakephp)

This plugin is a thin wrapper for `JsonView` that allows using [Fractal transformers](http://fractal.thephpleague.com/transformers/) for your API output. What is [Fractal](http://fractal.thephpleague.com/)?

> 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
------------

[](#requirements)

- CakePHP 5.x (use ~1.0 for CakePHP 3.x, ~2.0 for CakePHP 4.x)

Installation
------------

[](#installation)

You can install this plugin into your CakePHP application using [Composer](http://getcomposer.org).

```
composer require andrej-griniuk/cakephp-fractal-transformer-view
```

Usage
-----

[](#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](http://book.cakephp.org/4/en/views/json-and-xml-views.html) - 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
-------------------

[](#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](https://packagist.org/packages/cakephp/bake) composer package installed.

You can now run `bin/cake bake transformer YOUR_MODEL` to create transformers.

Bugs &amp; Feedback
-------------------

[](#bugs--feedback)

Credits
-------

[](#credits)

Inspired by @josegonzalez [Using Fractal to transform entities for custom api endpoints](http://josediazgonzalez.com/2015/12/01/using-fractal-to-transform-entities-for-custom-api-endpoints/).

License
-------

[](#license)

Copyright (c) 2016, [Andrej Griniuk](https://github.com/andrej-griniuk) and licensed under [The MIT License](http://www.opensource.org/licenses/mit-license.php).

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity39

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 88% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~710 days

Total

5

Last Release

890d ago

Major Versions

v1.0 → 2.02021-01-14

2.2 → 3.02023-12-11

PHP version history (4 changes)v1.0PHP &gt;=5.5.0

2.0PHP &gt;=7.2

2.2PHP &gt;=7.4

3.0PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/7a429c741847f43b7bfdc57ac33afaceaaeee837c694464357f893279398ef74?d=identicon)[andrej-griniuk](/maintainers/andrej-griniuk)

---

Top Contributors

[![andrej-griniuk](https://avatars.githubusercontent.com/u/2722793?v=4)](https://github.com/andrej-griniuk "andrej-griniuk (44 commits)")[![amayer5125](https://avatars.githubusercontent.com/u/3212673?v=4)](https://github.com/amayer5125 "amayer5125 (3 commits)")[![ADmad](https://avatars.githubusercontent.com/u/142658?v=4)](https://github.com/ADmad "ADmad (2 commits)")[![dakota](https://avatars.githubusercontent.com/u/83255?v=4)](https://github.com/dakota "dakota (1 commits)")

---

Tags

jsonapirestcakephpfractal

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/andrej-griniuk-cakephp-fractal-transformer-view/health.svg)

```
[![Health](https://phpackages.com/badges/andrej-griniuk-cakephp-fractal-transformer-view/health.svg)](https://phpackages.com/packages/andrej-griniuk-cakephp-fractal-transformer-view)
```

###  Alternatives

[mixerapi/mixerapi

Streamline development of API-first applications in CakePHP

4441.8k](/packages/mixerapi-mixerapi)[cakedc/cakephp-api

Api plugin for CakePHP

61100.6k](/packages/cakedc-cakephp-api)[sprintcube/cakephp-rest

Rest API plugin for CakePHP 3

256.6k](/packages/sprintcube-cakephp-rest)[jsor/hal-client

A lightweight client for consuming and manipulating Hypertext Application Language (HAL) resources.

2425.9k1](/packages/jsor-hal-client)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
