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