PHPackages                             nilportugues/json-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. nilportugues/json-api

ActiveLibrary[API Development](/categories/api)

nilportugues/json-api
=====================

Serializer transformers outputting valid API responses in JSON, JSON API and HAL+JSON API formats.

2.7.2(7y ago)70106.2k↑291.7%34[8 issues](https://github.com/nilportugues/php-json-api/issues)[1 PRs](https://github.com/nilportugues/php-json-api/pulls)3MITPHPPHP &gt;=5.5.0

Since Aug 15Pushed 7y ago5 watchersCompare

[ Source](https://github.com/nilportugues/php-json-api)[ Packagist](https://packagist.org/packages/nilportugues/json-api)[ Docs](http://nilportugues.com)[ RSS](/packages/nilportugues-json-api/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (79)Used By (3)

JSON API Transformer &amp; Server Helpers
=========================================

[](#json-api-transformer--server-helpers)

[![Build Status](https://camo.githubusercontent.com/14646ec89a7ae7d81566d6842827a2d5e9d4f7b26896ca88d398dc7f46074166/68747470733a2f2f7472617669732d63692e6f72672f6e696c706f727475677565732f7068702d6a736f6e2d6170692e737667)](https://travis-ci.org/nilportugues/php-json-api)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/cb6c58a62fb42b3281de067765c4e1684be54fab1d4b3739edf670a37b4cc865/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e696c706f727475677565732f6a736f6e2d6170692f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/nilportugues/json-api/?branch=master) [![SensioLabsInsight](https://camo.githubusercontent.com/6a4689a65d37e31caaa887fa275594082033974b1a56be79bac01826c0a90a68/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f65333965346330652d613430322d343935622d613736332d3665303438326532303833642f6d696e692e706e67)](https://insight.sensiolabs.com/projects/e39e4c0e-a402-495b-a763-6e0482e2083d) [![Latest Stable Version](https://camo.githubusercontent.com/17c99ffd774ded493a8d4efefd432e48d8a5991861e32ccdb4c89ccc7d0f404e/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f6a736f6e2d6170692f762f737461626c65)](https://packagist.org/packages/nilportugues/json-api) [![Total Downloads](https://camo.githubusercontent.com/734d4880a6169d039e5b64ba6ad53d2dc666e7bf8ef993bfbbf33252f75c0b5b/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f6a736f6e2d6170692f646f776e6c6f616473)](https://packagist.org/packages/nilportugues/json-api) [![License](https://camo.githubusercontent.com/bd8a30bf18599558cc326673b60d99cfafaede1e51415e6dcffafc0b08b627d9/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f6a736f6e2d6170692f6c6963656e7365)](https://packagist.org/packages/nilportugues/json-api)[![Donate](https://camo.githubusercontent.com/7b6de155df30b37b25eb5fec52f9213680c3dbf067dfb7d7e2850ac4096c7d05/68747470733a2f2f7777772e70617970616c6f626a656374732e636f6d2f656e5f55532f692f62746e2f62746e5f646f6e6174655f534d2e676966)](https://paypal.me/nilportugues)

- [Installation](#installation)
- [Transfomer Classes](#transfomer-classes)
- [Server Classes](#server-classes)
    - [JSON API Request object](#json-api-request-object)
    - [Request Object](#request-object)
    - [JSON API Response objects](#json-api-response-objects)
    - [Action Objects](#action-objects)

Installation
============

[](#installation)

Use [Composer](https://getcomposer.org) to install the package:

```
$ composer require nilportugues/json-api
```

Transfomer Classes
==================

[](#transfomer-classes)

Given a PHP Object, and a series of mappings, the **JSON API Transformer** will represent the given data following the `http://jsonapi.org` specification.

For instance, given the following piece of code, defining a Blog Post and some comments:

```
$post = new Post(
  new PostId(9),
  'Hello World',
  'Your first post',
  new User(
      new UserId(1),
      'Post Author'
  ),
  [
      new Comment(
          new CommentId(1000),
          'Have no fear, sers, your king is safe.',
          new User(new UserId(2), 'Barristan Selmy'),
          [
              'created_at' => (new DateTime('2015/07/18 12:13:00'))->format('c'),
              'accepted_at' => (new DateTime('2015/07/19 00:00:00'))->format('c'),
          ]
      ),
  ]
);
```

And a Mapping series of classes implementing `JsonApiMapping` interface.

```
