2017 © Pedro Peláez
 

library fractal-response

Using Transformers in Laravel Responses

image

jotaen/fractal-response

Using Transformers in Laravel Responses

  • Wednesday, June 10, 2015
  • by jotaen
  • Repository
  • 1 Watchers
  • 3 Stars
  • 39 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 15 % Grown

The README.md

FractalResponse for Laravel

Build Status Scrutinizer Code Quality Latest Release, (*1)

With FractalReponse you can easily use Fractal Transformers to serialize Laravel Reponse objects., (*2)

The FractalResponse class inherits from Illuminate\Http\Response. In addition, you pass your Leage\Fractal-transformer to the with()-method, so that your response gets automatically serialized the way you wish. This is especially helpful when writing REST services, where you have to provide a defined data structure., (*3)

Installation:

The easiest way is to add FractalResponse as dependency to your composer.json:, (*4)

require: {
    "jotaen/fractal-response": "1.x"
}

Example:

<?php namespace App\Http\Controllers;

use League\Fractal;
use Laravel\Lumen\Routing\Controller as BaseController;

use FractalResponse\FractalResponse as Response;

class Controller extends BaseController
{
    public function showOneBook()
    {
      $book = new Book();
      $response = new Response($book, 200);
      $response->with(new BookTransformer());
      return $response;
    }

    public function showSeveralBooks()
    {
      $books = [ new Book(), new Book() ];
      $response = new Response($books, 200);
      $response->with(new BookTransformer());
      return $response;
    }
}


class Book 
{
  public function title() { return 'Help, i am an elephant!'; }
}

class BookTransformer extends Fractal\TransformerAbstract
{
  public function transform($foo)
  {
      return [
          'Title'      => $foo->title(),
      ];
  }
}

The Versions

10/06 2015

dev-master

9999999-dev http://github.com/jotaen/fractal-response

Using Transformers in Laravel Responses

  Sources   Download

MIT

The Requires

 

laravel api json league response transform serialize fractal

09/06 2015

v1.0.0

1.0.0.0 http://github.com/jotaen/fractal-response

Using Transformers in Laravel Responses

  Sources   Download

MIT

The Requires

 

laravel api json league response transform serialize fractal