-
Notifications
You must be signed in to change notification settings - Fork 0
Response http documentation
This framework is compatible with Simfony, Laravel, Lumen and PHP >= 7.2
It is based upon one of the most used php library, that is http-foundation. It has been implemented additional functions for response handling, allowing the server to manage in an easy and efficient way:
- HTTP/2.0 standard responses with default content and code
- Content manipulation in a fast and dynamic way (also with REST standards)
- Adding response link with HATEOAS standard to content • Headers easy manipulation
- Etag easy generation (with code.key.date base64 encoded payload)
- Conditional request handling comparing it with the response
- (Laravel / Lumen) Trait to be added to models for mapping resource links
Response object creation
use ResponseHTTP\Response\HttpResponse;
//Costuctor
$response = new HttpResponse('Content', 200, array('content-type' => 'text/html'));
//Without costructor
$response = new HttpResponse();
return $response->successCreated();
Standard responses implemented from HttpResponse class are present in HttpResponseInterface.php interface
** Methods available ** handling response and his object creation
$response->withContent() //Adds a key: value element to the content
->withContents() //Adds a key: value elements to the content
->withLink() //Adds links:value with HATEOAS standard to content as default.
->withLinks() //Adds various values into links element of content (override disabled by default)
->withHeader() //Add a new header to the response
->withHeaders() //Add a new headers to the response
->withData() //Alias withContent for inclusion in the content 'data': value
Above functions are implemented into BaseHttpResponse.php class
$original property is an array containing response’s original
values, they are the content representation, this allows separate
handling and having an array key-value that contains content’s
protected data retrievable through the getOriginal()
function.
Furthermore it is possible to retrieve the response’s $content or
the $data property.
//Get the response property (you can also recover only one or more keys)
->getProperty()
////Get the response content from HttpFoundation\Response class (you can also recover only one or more keys)
->getContent()
////Get the response content from HttpFoundation\JsonResponse class (you can also recover only one or more keys)
->getData()
To capture exceptions and to return a coherent and correct response error to the client, you can use ResponseHTTP\Response\Exception\Handler classs, which handles exceptions and returns a JSON response to the client
//Laravel or Lumen
$this->app->singleton(
\Illuminate\Contracts\Debug\ExceptionHandler::class,
\ResponseHTTP\Response\Laravel\Exceptions\Handler::class
)
//PHP
try {
(new \ResponseHTTP\Response\Exceptions\Handler())->setExceptionHandler()
} catch (\Exception $e) {
//
}
//RESPONSE
{
'error': {
'message': message value of exception,
'code': code value of exception,
'status_code': 4xx,
'debug': {
'file': ...,
'line': ...,
'trace': ...,
}
}
* The debug element will be included only if into .env file the
RESPONSE_DEBUG value is set to true. ** The status_code element will be included only if the exception is of type HttpException, moreover also the exception headers will be appended to the response.
ErrorsException class allows to handle exceptions implementing methods for message construction and other. It is also possible to use this class to extend your ErrorsException.php exceptions
To create a personal response class, with personalized methods and functions, it is enough to create a new class.
namespace ...;
use ResponseHTTP\Response\BaseHttpResponse;
class MyResponse extends BaseHttpResponse {
//your method
}
__preCostructor() function allows in a very simple way to initialize the response (to see how to use it, see HttpResponse.php
$users = User::all(); //ex. with laravel
return (new HttpResponse())->data($users)
//RESPONSE
{
'data': {
{'id':1,'name':'test}
{'id':2,'name':'test2}
{'id':N,'name':'testN}
}
}
$users = User::all(); //ex. with laravel
return (new HttpResponse())->errorBadRequest()
//RESPONSE
{
'errors': 'Error Bad Request'
}