2017 © Pedro Peláez
 

library delegator

A nice API response helper, to build the output you want.

image

bycedric/delegator

A nice API response helper, to build the output you want.

  • Tuesday, June 23, 2015
  • by byCedric
  • Repository
  • 1 Watchers
  • 6 Stars
  • 20 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Delegator

A nice API helper, for Laravel, to keep your responses RESTed. It blends in with the usual Response of Laravel., (*1)

Installation

You can install this package through Composer., (*2)

"require-dev": {
    "by-cedric/delegator": "dev-master"
}

Delegator extends the basic Response facade of Laravel. So you need to use the Delegator's version of the Response facade. Ofcourse you can still use the standard functions of Response., (*3)

In your config/app.php search for this line:, (*4)

'aliases' => array(
    ...
    'Response'        => 'Illuminate\Support\Facades\Response',
    ...
)

And replace it with:, (*5)

'aliases' => array(
    ...
    'Response'        => 'ByCedric\Delegator\Facades\Response',
    ...
)

Basic Usage

You can use Delegator as simple as:, (*6)

Response::api();

This initiates a new Delegator, that is actually a Response object. So when you are done setting your response, you can just return it as a normal response:, (*7)

public function show( $id )
{
    $task = Task::find($id);

    return Response::api($task);
}

Functions

All functions are chainable. A new Delegator instance is created using the Response facade:, (*8)

Response::api();

You can use one of the following functions, as chained methods, to manipulate the response., (*9)

code

Within a normal response, there is always a status code to explain the basic situation., (*10)

It accepts an integer., (*11)

Response::api()->code(403);

message

An API response is often read by developers, when developing their application. During the development process, a lot can go wrong. So it is always recomended to provide a human readable message., (*12)

It accepts a string., (*13)

Response::api()->message('Please specify a valid API key.');

data

An API response is almost all the time filled with data. No need for explaination I think..., (*14)

It accepts an array, or any object that implements the ArrayableInterface., (*15)

Response::api()->data($task);

Note that the constructor of the Delegator also accepts data, exactly the same way., (*16)

Response::api($task);

callback

Not all the time a simple JSON request can be made. Then you will just have to use the JSONP response., (*17)

It accepts a string., (*18)

Response::api()->callback('loaded_call');

error

When an error occures, you probably want it responded asap. The error takes care of the human readable message and the http status code., (*19)

It accepts a string and integer., (*20)

Response::api()->error('Could not find task with id #'. $id, 404);

limit

When responding with a collection, info about the amount of items that can be returned at once is very useful. For example, you can build your pagination with it., (*21)

It accepts an integer, or boolean to remove any set value., (*22)

Response::api()->limit(100);

offset

When responding with a collection, info about the current offset of the total collection is very useful. For example, you can build your pagination with it., (*23)

Response::api()->offset(45);

count

When responding with a collection, info about the total amount of items in existence is very useful. For example, you can build your pagination with it., (*24)

Response::api()->count(231);

mockCode

Some (old) clients are very clumsy with errors in the http status code. When this is the case, it is best to let the client relax for a bit. This will always pass http status code 200, only the code within the response is the real one., (*25)

It accepts a boolean, mostly used to disable it again., (*26)

Response::api()->code(404)->mockCode();

The Versions

23/06 2015

dev-develop

dev-develop http://github.com/bycedric/delegator

A nice API response helper, to build the output you want.

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

api jsonapi delegator

23/06 2015

dev-deprecated

dev-deprecated http://github.com/bycedric/delegator

A nice API helper, for Laravel, to keep your responses RESTed.

  Sources   Download

MIT

The Requires

 

laravel api rest delegator

22/03 2014

dev-master

9999999-dev http://github.com/bycedric/delegator

A nice API helper, for Laravel, to keep your responses RESTed.

  Sources   Download

MIT

The Requires

 

laravel api rest delegator