bycedric/delegator

This package is abandoned and no longer maintained. No replacement package was suggested.

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

dev-master 2014-03-22 13:08 UTC

This package is not auto-updated.

Last update: 2020-01-24 15:21:56 UTC


README

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

Installation

You can install this package through Composer.

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

In your config/app.php search for this line:

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

And replace it with:

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

Basic Usage

You can use Delegator as simple as:

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:

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:

Response::api();

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

code

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

It accepts an integer.

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.

It accepts a string.

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

It accepts an array, or any object that implements the ArrayableInterface.

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

Note that the constructor of the Delegator also accepts data, exactly the same way.

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.

It accepts a string.

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.

It accepts a string and integer.

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.

It accepts an integer, or boolean to remove any set value.

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.

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.

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.

It accepts a boolean, mostly used to disable it again.

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