Skip to content

Latest commit

 

History

History
85 lines (54 loc) · 2.09 KB

README.md

File metadata and controls

85 lines (54 loc) · 2.09 KB

jsonapi Build Status

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.

Getting started

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.

Upgrading from v1

If you used v1 of this library, see /UPGRADE_1_TO_2.md on how to upgrade.

Installation

Use Composer. And use require to get the latest stable version:

composer require alsvanzelf/jsonapi

Features

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)

Contributing

Pull Requests or issues are welcome!

Licence

MIT