2017 © Pedro Peláez
 

library responder

A Laravel 4 wrapper for Fractal to handle API responses

image

divspace/responder

A Laravel 4 wrapper for Fractal to handle API responses

  • Thursday, November 5, 2015
  • by divspace
  • Repository
  • 1 Watchers
  • 0 Stars
  • 75 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 1 % Grown

The README.md

Responder

A Laravel 4 wrapper for Fractal to handle API responses. For Laravel 5, please go here., (*1)

Installation

Add Responder to your composer.json file by running the following command:, (*2)

$ composer require divspace/responder

Then run the following command:, (*3)

$ composer update

Register Package

Add the following line to the end of the providers array in app/config/app.php:, (*4)

'providers' => array(
    // ...
    'Divspace\Responder\ResponderServiceProvider'
)

Usage

There are three types of responses you can return:, (*5)

  1. Item
  2. Collection
  3. Paginated Collection

Here are examples of all three types in a single controller:, (*6)

```php use Divspace\Responder\Responder; use Data\Transformers\UserTransformer;, (*7)

class UserController extends BaseController { protected $response;, (*8)

public function __construct(Responder $response) {
    $this->response = $response;

    /**
     * Fractal parseIncludes() method
     * http://fractal.thephpleague.com/transformers/
     */
    if(isset($_GET['include'])) {
        $this->response->parseIncludes($_GET['include']);
    }
}

/**
 * Display a paginated list of users
 */
public function index() {
    return $this->response->paginatedCollection(User::paginate());
}

/**
 * Display a single user
 */
public function item($id) {
    return $this->response->item(User::find($id), new UserTransformer());
}

/**
 * Display all users
 */
public function collection() {
    return $this->response->collection(User::all(), new UserTransformer());
}

}, (*9)


The `Data\Transformers\UserTransformers` file might look something like this (with an example of how to use the `parseIncludes()` method from the previous example): ```php <?php namespace Data\Transformers; use User; use League\Fractal\TransformerAbstract; class UserTransformer extends TransformerAbstract { protected $defaultIncludes = [ 'user_type' ]; public function transform(User $user) { $user = User::find($user->id); return [ 'id' => (int) $user->id, 'email' => $user->email, 'created_at' => $user->created_at, 'updated_at' => $user->updated_at, ]; } public function includeUserType(User $user) { $userType = $user->type; return $this->item($userType, new UserTypeTransformer); } }

Since we've used the parseIncludes() method in the previous example, we need to create that file as well so we can return more information in our API response:, (*10)

<?php namespace Data\Transformers;

use User;
use League\Fractal\TransformerAbstract;

class UserTypeTransformer extends TransformerAbstract {

    public function transform($userType) {
        if(count($userType) > 0) {
            return [
                'id'   => (int) $userType->id,
                'name' => $userType->name
            ];
        }

        return [];
    }

}

You'll want to refer to the full Fractal documentation to get an idea of how everything works together., (*11)

Credit

This is modified fork of Larasponse for Laravel 4.2 that uses Fractal as the default provider for API responses., (*12)

The Versions

05/11 2015

dev-master

9999999-dev

A Laravel 4 wrapper for Fractal to handle API responses

  Sources   Download

MIT

The Requires

 

laravel api json rest league fractal

05/11 2015

v1.0.1

1.0.1.0

A Laravel 4 wrapper for Fractal to handle API responses

  Sources   Download

MIT

The Requires

 

laravel api json rest league fractal

23/10 2015

v1.0.0

1.0.0.0

Laravel wrapper for Fractal

  Sources   Download

MIT

The Requires