XBLD / laravel-api-response by KalebClark

Response for restful API
15
1
2
Package Data
Maintainer Username: KalebClark
Maintainer Contact: kaleb@abraxxus.net (Kaleb Clark)
Package Create Date: 2016-06-30
Package Last Update: 2016-07-01
Language: PHP
License: Unknown
Last Refreshed: 2025-02-13 15:09:43
Package Statistics
Total Downloads: 15
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 1
Total Watchers: 2
Total Forks: 1
Total Open Issues: 0

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.

Feature List:

  • Standardized responses for all requests.
  • Ability to log debug messages to response for easier debugging
Install
$ composer require xbld/laravel-api-response
Setup

Open config\app.php and add this to the providers array:

XBLD\ApiResponse\ApiResponseServiceProvider::class,

Dont forget to dump the autoload

$ 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:

use XBLD\ApiResponse\APIResponse;

Example Method

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:

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