GithubHelp home page GithubHelp logo

jeffersonsimaogoncalves / cakephp-fractal-transformer-view Goto Github PK

View Code? Open in Web Editor NEW

This project forked from andrej-griniuk/cakephp-fractal-transformer-view

9.0 3.0 0.0 25 KB

CakePHP view builder utilizing Fractal library for entities transformation

License: MIT License

PHP 100.00%
cakephp cakephp3x cakephp-view cakephp-plugin

cakephp-fractal-transformer-view's Introduction

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

Installation

You can install this plugin into your CakePHP application using Composer.

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

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

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

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

You can also define if you don't want to use transformer for certain variables:

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

Bugs & Feedback

https://github.com/jeffersonsimaogoncalves/cakephp-fractal-transformer-view/issues

Credits

This work is based on the code by Andrej Griniuk.

cakephp-fractal-transformer-view's People

Contributors

andrej-griniuk avatar jeffersonsimaogoncalves avatar dakota avatar

Stargazers

xdrps avatar Oybek Kayumov avatar Gábor Mihálcz avatar Gabriel Guimarães avatar  avatar ab25cq avatar  avatar Edivaldo Junior avatar  avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.