Build a better REST API response.
use Luur\Restponder\Restponder;
$response = Restponder::content('happy little 🌳');
{
"success":true,
"result":"happy little \ud83c\udf33",
"error":null
}
use Luur\Restponder\Restponder;
$response = Restponder::content('happy little 🌳');
$response->addMetadata('request_id', '1234-5678');
{
"success":true,
"result":"happy little \ud83c\udf33",
"error":null,
"metadata":{
"request_id":"1234-5678"
}
}
use Luur\Restponder\Restponder;
$response = Restponder::content(new Exception('Oops', 987));
{
"success":false,
"result":null,
"error":{
"code":987,
"message":"Oops"
}
}
use Luur\Restponder\Restponder;
Restponder::setErrorIncludeDebug(true);
$response = Restponder::content(new Exception('Oops', 987));
{
"success":false,
"result":null,
"error":{
"code":987,
"message":"Oops",
"debug":{
"type":"Exception",
"trace":"#0 ..."
}
}
}
use Luur\Restponder\ErrorData;
use Luur\Restponder\Restponder;
$handler = function (Exception $exception, ErrorData $data) {
$data->addDetail('is_validation_exception', $exception instanceof ValidationException);
};
Restponder::registerErrorHandler(Exception::class, $handler);
$response = Restponder::content(new Exception('Oops', 987));
{
"success":false,
"result":null,
"error":{
"code":0,
"message":"Unexpected error occurred",
"details":{
"is_validation_exception":false
}
}
}
You can register a custom object handler, to be able to parse response data from any kind of object, however you like.
use Luur\Restponder\ResponseContent;
use Luur\Restponder\Restponder;
$handler = function (Exception $exception, ResponseContent $response) {
$response->addMetadata('failed', true);
$response->addMetadata('error_message', $exception->getMessage());
};
Restponder::registerResponseHandler(Exception::class, $handler);
You can register a custom object handler, just like with Response
.
use Luur\Restponder\ErrorData;
use Luur\Restponder\Restponder;
$handler = function (Exception $exception, ErrorData $data) {
$data->setMessage($exception->getCode().$exception->getMessage());
$data->addDetail('test', true);
};
Restponder::registerErrorHandler(Exception::class, $handler);