deralia/json-api

JsonAPI standard implementation

1.0.0 2021-05-21 16:50 UTC

README

This project is an updated Fork of the mikemirten/json-api library.
Credits for the code base to the original author.

This repository contains PHP-implementation of the JsonAPI standard.

An integration with the Symfony Framework can be found inside of JsonApi-Bundle repository.

Install

Through composer:

composer require deralia/json-api

How to use (original Wiki)

Overview

Code example:

use Deralia\Component\JsonApi\Document\ResourceObject;
use Deralia\Component\JsonApi\Document\SingleResourceDocument;

// ...

$post = $postRepository->findById($id);

$resource = new ResourceObject($id, 'Post', [
    'title' => $post->getTitle(),
    'body'  => $post->getBody()
]);

$document = new SingleResourceDocument($resource);

echo json_encode($document->toArray());

Response body:

{
    "data": {
        "id": "1",
        "type": "Post",
        "attributes": {
            "title": "Lorem Ipsum",
            "body": "Lorem ipsum dolor sit amet, lobortis urna sed imperdiet..."
        }
    }
}