Laravel REST API Response Builder


October 31st, 2019


REST API Response Builder for Laravel is a package by Marcin Orlowski for building JSON API responses. According to the author, the package helps you with things like data conversion and localization:

ResponseBuilder is written for REST API developers by REST API developer and is based on my long-lasting experience on both “sides” (API dev and API consumer) of a variety of REST APIs. Lightweight, with simple to use public methods, covering multiple potential use-cases, on-the-fly data conversion, localization support, automatic error message building, support for chained APIs, and (hopefully) exhaustive documentation.

The simplest example provided by this package is returning the following from a controller:

1return ResponseBuilder::success();

The resulting client JSON will look like the following:

2 “success”: true,
3 “code”: 0,
4 “locale”: “en”,
5 “message”: “OK”,
6 “data”: null

If you want to build message codes for common errors in your API, you can do the following:

1return ResponseBuilder::error(MyErrorCodes::SOME_CODE);

Which results in the following message if you passed the code 205:

2 “success”: false,
3 “code”: 250,
4 “locale”: “en”,
5 “message”: “Your error message for code 250,
6 “data”: null

Check out the detailed documentation for more examples of what this package provides. You can learn more about this package, get full installation instructions, and view the source code on GitHub at MarcinOrlowski/laravel-api-response-builder.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.

Laravel News Partners