2017 © Pedro Peláez
 

cakephp-plugin cakephp-fractal-transformer-view

CakePHP view builder utilizing Fractal library for entities transformation

image

jeffersonsimaogoncalves/cakephp-fractal-transformer-view

CakePHP view builder utilizing Fractal library for entities transformation

  • Friday, June 8, 2018
  • by jeffersonsimaogoncalves
  • Repository
  • 1 Watchers
  • 1 Stars
  • 18 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 7 Versions
  • 350 % Grown

The README.md

FractalTransformerView plugin for CakePHP

This plugin is a thin wrapper for JsonView that allows using Fractal transformers for your API output. What is Fractal?, (*1)

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., (*2)

Requirements

  • CakePHP 3.6+

Installation

You can install this plugin into your CakePHP application using Composer., (*3)

composer require jeffersonsimaogoncalves/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 var. E.g.:, (*4)

namespace App\Controller;

class ArticlesController extends AppController
{
    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('RequestHandler');

        $this->viewBuilder()->className('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->set('_serialize', ['articles']);
    }
}

The view will look for transformer class starting with entity name. E.g.:, (*5)

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., (*6)

Custom transformer class name can be set by defining _transformer view var:, (*7)

$this->set('_transform', ['articles' => '\App\Model\Transformer\CustomArticleTransformer']);

You can also define if you don't want to use transformer for certain variables:, (*8)

$this->set('_transform', ['articles' => false]);

Bugs & Feedback

https://github.com/jeffersonsimaogoncalves/cakephp-fractal-transformer-view/issues, (*9)

Credits

This work is based on the code by Andrej Griniuk., (*10)

The Versions

08/06 2018

dev-master

9999999-dev http://github.com/jeffersonsimaogoncalves/cakephp-fractal-transformer-view

CakePHP view builder utilizing Fractal library for entities transformation

  Sources   Download

MIT

The Requires

 

by Andrej Griniuk

api json cakephp rest fractal

08/06 2018

v1.2.3

1.2.3.0 http://github.com/jeffersonsimaogoncalves/cakephp-fractal-transformer-view

CakePHP view builder utilizing Fractal library for entities transformation

  Sources   Download

MIT

The Requires

 

by Andrej Griniuk

api json cakephp rest fractal

08/06 2018

v1.2.2

1.2.2.0 http://github.com/jeffersonsimaogoncalves/cakephp-fractal-transformer-view

CakePHP view builder utilizing Fractal library for entities transformation

  Sources   Download

MIT

The Requires

 

by Andrej Griniuk

api json cakephp rest fractal

01/06 2018

v1.2.1

1.2.1.0 http://github.com/jeffersonsimaogoncalves/cakephp-fractal-transformer-view

CakePHP view builder utilizing Fractal library for entities transformation

  Sources   Download

MIT

The Requires

 

by Andrej Griniuk

api json cakephp rest fractal

26/05 2018

v1.2

1.2.0.0 http://github.com/jeffersonsimaogoncalves/cakephp-fractal-transformer-view

CakePHP view builder utilizing Fractal library for entities transformation

  Sources   Download

MIT

The Requires

 

by Andrej Griniuk

api json cakephp rest fractal

25/05 2018

v1.1

1.1.0.0 http://github.com/jeffersonsimaogoncalves/cakephp-fractal-transformer-view

CakePHP view builder utilizing Fractal library for entities transformation

  Sources   Download

MIT

The Requires

 

by Andrej Griniuk

api json cakephp rest fractal

29/02 2016

v1.0

1.0.0.0 http://github.com/andrej-griniuk/cakephp-fractal-transformer-view

CakePHP view builder utilizing Fractal library for entities transformation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrej Griniuk

api json cakephp rest fractal