New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

divspace/responder

A Laravel 4 wrapper for Fractal to handle API responses
163

Larablocks Analysis

divspace/responder works with Laravel 4 (latest release v1.0.1, Nov 5, 2015), which reached end of life: no more fixes, even for security.

Recommendation checks

Not advised

Not advised: no release or commit in over 2 years.

  • No release or commit in over 2 years
  • Supports only Laravel 4, past end of life
  • No release in the last 12 months (last: Nov 5, 2015)
  • 5 downloads a month (needs 10,000)
  • 0 GitHub stars (needs 100)

State

Likely abandoned
  • No release or commit in over 2 years.
  • Last stable release: Nov 5, 2015.
  • No releases in the last 12 months.
  • First released: Oct 23, 2015.
  • Monthly downloads: 5.
  • 0 open issues on GitHub.

Data from Packagist and GitHub, checked by Larablocks on Dec 4, 2026.

Install
composer require divspace/responder
Latest Version:v1.0.1
PHP:>=5.4.0
Laravel:4
License:MIT
Last Updated:Nov 11, 2015
Links: GitHub  ·  Packagist
Maintainer: divspace

Responder

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

Installation

Add Responder to your composer.json file by running the following command:

$ composer require divspace/responder

Then run the following command:

$ composer update

Register Package

Add the following line to the end of the providers array in app/config/app.php:

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

Usage

There are three types of responses you can return:

  1. Item
  2. Collection
  3. Paginated Collection

Here are examples of all three types in a single controller:

use Divspace\Responder\Responder;
use Data\Transformers\UserTransformer;

class UserController extends BaseController {
    protected $response;

    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());
    }
}

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

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

Credit

This is modified fork of Larasponse for Laravel 4.2 that uses Fractal as the default provider for API responses.

Alternatives to divspace/responder

league/fractal

Handle the output of complex data structures ready for API output.

73,372,068 3,535
php-open-source-saver/fractal

Handle the output of complex data structures ready for API output.

672,619 4
ellipsesynergie/api-response

Simple package to handle response properly in your API

1,444,398 370
givebutter/laravel-keyable

Add API keys to your Laravel models

184,362 189
chrisbjr/api-guard

A simple way of authenticating your APIs with API keys using Laravel

377,407 690

Version History

Version Released PHP Laravel License
v1.0.1 >=5.4.0 4.2.* MIT
v1.0.0 >=5.4.0 4.2.* MIT