Skip to content

json-api-php/json-api

Repository files navigation

Implementation of JSON API in PHP 7

This library is an attempt to express business rules of JSON API specification in a set of PHP 7 classes.

A simple example to illustrate the general idea. This JSON representation from the documentation

{
    "data": {
        "type": "articles",
        "id": "1",
        "attributes": {
            "title": "Rails is Omakase"
        },
        "relationships": {
            "author": {
                "data": {
                    "type": "people",
                    "id": "9"
                },
                "links": {
                    "self": "/articles/1/relationships/author",
                    "related": "/articles/1/author"
                }
            }
        }
    }
}

can be built with the following php code (less imports):

<?php
$articles = new \JsonApiPhp\JsonApi\Document\Resource\ResourceObject('articles', '1');
$author = \JsonApiPhp\JsonApi\Document\Resource\Relationship::fromLinkage(
    new \JsonApiPhp\JsonApi\Document\Resource\Linkage\SingleLinkage(
        new \JsonApiPhp\JsonApi\Document\Resource\ResourceIdentifier('people', '9')
    )
);
$author->setLink('self', '/articles/1/relationships/author');
$author->setLink('related','/articles/1/author');
$articles->setRelationship('author', $author);
$articles->setAttribute('title', 'Rails is Omakase');
$doc = \JsonApiPhp\JsonApi\Document::fromResource($articles);
echo json_encode($doc, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

Please refer to the tests for the full API documentation:

Installation

With composer: json-api-php/json-api.