Skip to content

Instantly share code, notes, and snippets.

@galpratama
Created February 19, 2020 11:18
Show Gist options
  • Save galpratama/36ca7e903a73bc46acaa6fb810b7cd13 to your computer and use it in GitHub Desktop.
Save galpratama/36ca7e903a73bc46acaa6fb810b7cd13 to your computer and use it in GitHub Desktop.
API Response Formatter
<?php
namespace App\Http\Controllers\API;
class ResponseFormatter
{
protected static $response = [
'meta' => [
'code' => 200,
'status' => 'success',
'message' => null
],
'data' => null
];
public static function success($data = null, $message = null)
{
self::$response['meta']['message'] = $message;
self::$response['data'] = $data;
return response()->json(self::$response, self::$response['meta']['code']);
}
public static function error($data = null, $message = null, $code = 400)
{
self::$response['meta']['status'] = 'error';
self::$response['meta']['code'] = $code;
self::$response['meta']['message'] = $message;
self::$response['data'] = $data;
return response()->json(self::$response, self::$response['meta']['code']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment