2017 © Pedro Peláez
 

library api-response

Hypermedia response for Laravel Restful API

image

lowfrequency/api-response

Hypermedia response for Laravel Restful API

  • Friday, March 30, 2018
  • by lowfrequency
  • Repository
  • 0 Watchers
  • 0 Stars
  • 8 Installations
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Laravel API Response

For Restful API's

Laravel does a great job formatting our responses into JSON and kicking them out appropriatly with application/json content types, but it does not help with any type of standard output format such as having a "status", or "messages" in the response. This class allows us to standardize all of our restful responses making our frontend friends much happier. Also helps with unit testing by having a true/false for each response., (*1)

Feature List:, (*2)

  • Standardized responses for all requests.
Install
$ composer require lowfrequency/api-response
Setup

Open config\app.php and add this to the providers array:, (*3)

LowFrequency\ApiResponse\ApiResponseServiceProvider::class,

Dont forget to dump the autoload, (*4)

$ composer dump-autoload
Usage

This is intended to be used inside your controllers on a per method basis. Add the following to the top of your controller:, (*5)

use LowFrequency\ApiResponse\APIResponse;

Example Method, (*6)

public function store(Request $request)
{
    $return = new APIResponse();

    // If something fails on execution, Maybe a query does not
    // return anything...
    $return->status = false;
    $return->addMessage("Model Not Found");


    // Add the payload. Can be any array and will return JSON
    $return->payload = [
        'foo'   => 'bar',
        'bar'   => true
    ];

    // Return the response.
    return $return->response();
}

Example Output:, (*7)

{
    "status": false,
    "messages": [
        "Model Not Found"
    ],
    "data": {
        [
            "foo": "bar",
            "bar": true
        ]
    },
    "completed_at": "2016-06-06 12:06:33"
}

The Versions

30/03 2018

dev-master

9999999-dev https://bitbucket.org/lowfrequency/api-response

Hypermedia response for Laravel Restful API

  Sources   Download

by Low Frequency

28/09 2017

0.0.0

0.0.0.0 https://bitbucket.org/lowfrequency/api-response

Hypermedia response for Laravel Restful API

  Sources   Download

by Low Frequency