A simple and human-friendly library for api servers (php serving json).
It allows you to generate json output according to the JSON:API standard, while being easy to understand for people without knowledge of the jsonapi standard.
The JSON:API standard makes it easy for clients to fetch multiple resources in one call and understand the relations between them. Read more about it at jsonapi.org.
A small example:
use alsvanzelf\jsonapi\ResourceDocument;
$document = new ResourceDocument($type='user', $id=42);
$document->add('name', 'Zaphod Beeblebrox');
$document->add('heads', 2);
$document->sendResponse();
Which will result in:
{
"jsonapi": {
"version": "1.0"
},
"data": {
"type": "user",
"id": "42",
"attributes": {
"name": "Zaphod Beeblebrox",
"heads": 2
}
}
}
You can also send collections (where data
is an array of resources) or errors (even automatically by exceptions).
Examples for all kind of responses are in the /examples directory.
If you used v1 of this library, see /UPGRADE_1_TO_2.md on how to upgrade.
Use Composer. And use require to get the latest stable version:
composer require alsvanzelf/jsonapi
This library handles all the basics:
- generating single resource documents
- generating resource collection documents
- adding to-one and to-many relationships
- generating errors documents (easily turning thrown exceptions into jsonapi output)
- sending out the json response with the correct http headers
Plans for the future include:
- support v1.1 of the specification
- handle creating, updating and deleting resources (#5)
Pull Requests or issues are welcome!