Created
February 19, 2020 11:18
-
-
Save galpratama/36ca7e903a73bc46acaa6fb810b7cd13 to your computer and use it in GitHub Desktop.
API Response Formatter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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