ajangi/php-rest-response

1.1.2 2021-02-18 17:25 UTC

This package is auto-updated.

Last update: 2024-10-24 16:07:28 UTC


README

Total Downloads Latest Stable Version License License

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 IceResponse\IceResponse;
...

public function index(): Response
    {
        $status_code = Response::HTTP_OK;
        $result = IceResponse::SUCCESS_RESPONSE;
        $data = ['users' => "List of users for example"];
        $messages = [];
        $developer_message = '';
        $response = new IceResponse($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 IceResponse\IceResponse;
...

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 IceResponse\IceResponse;
...

public function index(): Response
    {
        $status_code = Response::HTTP_NOT_FOUND;
        $result = IceResponse::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 IceResponse($status_code,$result,$data,$messages,$developer_message);
        return $response->send();
    }
// or simply use 
...
use Symfony\Component\HttpFoundation\Response;
use IceResponse\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 IceResponse\IceResponse;
use Symfony\Component\HttpFoundation\Response;

class ForbiddenErrorResponse extends IceResponse
{
    public function __construct()
    {
        $status_code = Response::HTTP_FORBIDDEN;
        $result = IceResponse::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 IceResponse\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": []
}

Contributing

Take a look at CONTRIBUTING for details.