dept-eduk/json-response

There is no license information available for the latest version (3.0.0) of this package.

3.0.0 2019-10-31 10:04 UTC

This package is not auto-updated.

Last update: 2024-10-12 08:02:18 UTC


README

This package provides a response wrapper that adds functionality for a json api.

Pulling in with composer:

  1. Install the package: composer require e3creative/json-response

Usage

You can use Laravel's Response class as normal with the additional JSON methods this package provides, e.g.

Response::empty();
Response::created($data);
Response::success($message, $status);
Response::error($message, $status, $data);
Response::unprocessable($errors);
public function index(Response $response) {
    return $response->empty();
}

If you follow step two of the installation instructions above, you can also use these methods with the response() function:

json_response()->empty();
json_response()->created($data);
json_response()->success($message, $status);
json_response()->error($message, $status, $data);
json_response()->unprocessable($errors);

Error response codes

The error() method expects a four digit response code. This will be transformed into three digit HTTP response code for the request, but the four digit response code will be returned in the response body. This allows for more granular error response identifcation in app/frontends.