PHP code example of hasandotprayoga / format-response

1. Go to this page and download the library: Download hasandotprayoga/format-response library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hasandotprayoga / format-response example snippets


    class Controller extends BaseController
    {
        use \FormatResponse\FormatResponse; // add this line
    }  
    

    public function render($request, Exception $exception)
    {
        if($exception instanceof ValidationException) {
            return parent::render($request, $exception);
        }

        $rendered = parent::render($request, $exception);
            
        $error = [
            'error'=>$exception->getMessage(),
            'file'=>$exception->getFile(),
            'line'=>$exception->getLine()
        ];

        $controller = app('\App\Http\Controllers\Controller');
        $controller->responseCode = $rendered->getStatusCode();
        $controller->messages = $error;

        return $controller->response();
    }
    
json
{
    "code": 200,
    "description": "Ok",
    "response": {
        "results": [
            {
                "id": 1,
                "balance": 100000,
                "recStatus": "DELETE",
                "recTimeCreate": "2019-01-16 17:10:01",
                "recTimeUpdate": "2019-01-29 04:39:22"
            },
            {
                "id": 2,
                "balance": null,
                "recStatus": "PUBLISH",
                "recTimeCreate": "2018-04-12 23:36:03",
                "recTimeUpdate": null
            },
            {
                "id": 3,
                "balance": null,
                "recStatus": "PUBLISH",
                "recTimeCreate": "2018-04-12 23:10:03",
                "recTimeUpdate": null
            },
        ],
        "messages": "ok",
        "metaData": {
            "selectedPage": 1,
            "selectedItem": null,
            "totalPage": 1,
            "totalItem": 3,
            "totalItemPerPage": 3
        }
    },
    "request": {
        "get": {
            "id": "1"
        }
    },
}