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:

return ResponseBuilder::success();

The resulting client JSON will look like the following:

  “success”: true,
  “code”: 0,
  “locale”: “en”,
  “message”: “OK”,
  “data”: null

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

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

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

   “success”: false,
   “code”: 250,
   “locale”: “en”,
   “message”: “Your error message for code 250”,
   “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.