snappdoctor / php-rest-response
1.1.1
2021-02-17 23:05 UTC
Requires
- php: >=7.0.0
- ext-json: *
- symfony/http-foundation: >=5.0
This package is auto-updated.
Last update: 2024-11-18 07:11:58 UTC
README
PHP-REST-RESPONSE
A PHP standard response structure to unify responses between microservices.
Requirements
- minimum php version : 7.0.0
Installation
composer require ajangi/php-rest-response
Hot to use?
Success Response Sample
... use Symfony\Component\HttpFoundation\Response; use DrResponse\DrResponse; ... public function index(): Response { $status_code = Response::HTTP_OK; $result = DrResponse::SUCCESS_RESPONSE; $data = ['users' => "List of users for example"]; $messages = []; $developer_message = ''; $response = new DrResponse($status_code,$result,$data,$messages,$developer_message); return $response->send(); }
The above code simply returens
{ "status_code": 200, "result": "SUCCESS", "developer_message": "", "messages": [], "data": { "users": "List of users for example" } }
Or you can simply use
... use Symfony\Component\HttpFoundation\Response; use DrResponse\DrResponse; ... public function index(): Response { return (new SuccessResponse())->send(); }
to get
{ "status_code": 200, "result": "SUCCESS", "developer_message": "", "messages": [], "data": [] }
Error Response Sample
... use Symfony\Component\HttpFoundation\Response; use DrResponse\DrResponse; ... public function index(): Response { $status_code = Response::HTTP_NOT_FOUND; $result = DrResponse::ERROR_RESPONSE; $data = []; $messages = [ 'entity' => ['entity not found!'] // This structure is recommended ]; $developer_message = 'Dear Front-End developer! You may have a typo!'; $response = new DrResponse($status_code,$result,$data,$messages,$developer_message); return $response->send(); }
// or simply use ... use Symfony\Component\HttpFoundation\Response; use DrResponse\NotFoundErrorResponse; ... public function index(): Response { $messages = [ 'entity' => ['entity not found!'] // This structure is recommended ]; $developer_message = 'Dear Front-End developer! You may have a typo!'; return (new NotFoundErrorResponse($messages, $developer_message))->send(); }
The above code snippets simply returns :
{ "status_code": 404, "result": "ERROR", "developer_message": "Dear Front-End developer! You may have a typo!", "messages": { "entity": [ "entity not found!" ] }, "data": [] }
Extending new Classes
For example assume you want to define a class for 403 Access Denied Response
// define a new class like this <?php namespace App; use DrResponse\DrResponse; use Symfony\Component\HttpFoundation\Response; class ForbiddenErrorResponse extends DrResponse { public function __construct() { $status_code = Response::HTTP_FORBIDDEN; $result = DrResponse::ERROR_RESPONSE; $data = []; $messages = [ 'access' => [ 'Forbidden!!' ] ]; $developer_message = ''; parent::__construct($status_code, $result, $data, $messages, $developer_message); } }
Using ForbiddenErrorResponse class :
... use Symfony\Component\HttpFoundation\Response; use DrResponse\NotFoundErrorResponse; ... public function index(): Response { return (new ForbiddenErrorResponse())->send(); }
and you'll get response :
{ "status_code": 403, "result": "ERROR", "developer_message": "", "messages": { "access": [ "Forbidden!!" ] }, "data": [] }