Skip to content

Commit

Permalink
fix json response
Browse files Browse the repository at this point in the history
  • Loading branch information
MRizki28 committed Aug 8, 2024
1 parent 6c5d8b1 commit 9acf701
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor
vendor
composer.lock
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"minimum-stability": "stable",
"prefer-stable": true,
"require": {
"php": "^8.2"
"php": "^8.2",
"symfony/http-foundation": "^7.1"
}
}
20 changes: 12 additions & 8 deletions src/ApiResponse/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

namespace MRizki28\ApiResponse;

class ApiResponse{
use Symfony\Component\HttpFoundation\Response;

class ApiResponse
{

/**
* Format a successful response.
Expand All @@ -14,14 +17,15 @@ class ApiResponse{
* @return string JSON formatted response.
*/

public static function success($data = [], $message = 'Success', int $code = 200){
http_response_code((int)$code);

header('Content-Type: application/json');
return json_encode([
public static function success($data = [], $message = 'Success', int $code = 200)
{
return new Response(json_encode([
'status' => 'success',
'message' => $message,
'data' => $data
]);
]),
$code,
['Content-Type' => 'application/json']
);
}
}
}

0 comments on commit 9acf701

Please sign in to comment.