Simple package to handle response properly in your API.
Simple package to handle response properly in your API.
Simple package to handle response properly in your API. This package uses Fractal and is based on Build APIs You Won't Hate book.
Via Composer
$ composer require ellipsesynergie/api-response| Version | Branch | PHP | Laravel |
|---|---|---|---|
| 2.x | master |
8.3 - 8.5 | 11 - 13 |
| 1.x | 1.x |
8.1 - 8.3 | 8 - 10 |
Version 2.x removed Lumen support, since Lumen is no longer maintained. If you need Lumen, stay on 1.x.
The service provider is auto-discovered. If you have disabled package discovery, add this following service provider to your config/app.php file.
EllipseSynergie\ApiResponse\Laravel\ResponseServiceProvider::classThis package can be used in any framework or vanilla php. You simply need to extend EllipseSynergie\ApiResponse\AbstractResponse and implement the withArray() method in your custom class.
You can take a look at EllipseSynergie\ApiResponse\Laravel\Response::withArray() for an example.
You will also need to instantiate the response class with a fractal manager instance.
// Instantiate the fractal manager
$manager = new \League\Fractal\Manager;
// Set the request scope if you need embed data
$manager->parseIncludes(explode(',', $_GET['include']));
// Instantiate the response object, replace the class name by your custom class
$response = new \EllipseSynergie\ApiResponse\AbstractResponse($manager);For more options related to the fractal manager, you can take a look at the official Fractal website
…{
"data": {
"id": 1,
"title": "My name is Bob!.",
"created_at": {
"date": "2014-03-25 18:54:18",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2014-03-25 18:54:18",
"timezone_type": 3,
"timezone": "UTC"
},
"deleted_at": null
}
}…{
"error": {
"code": "GEN-NOT-FOUND",
"http_code": 404,
"message": "Book Not Found"
}
}$ phpunitPlease see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.
No open issues yet, or sync has not completed.