|
1 | | -# jsonapi |
| 1 | +# jsonapi [](https://travis-ci.org/lode/jsonapi) |
2 | 2 |
|
3 | | -Simple and friendly library for api servers (PHP serving out JSON). |
| 3 | +A simple and human-friendly library for api servers (php serving json). |
| 4 | + |
| 5 | +It allows you to generate json output according to the [JSON:API](https://jsonapi.org/) standard, |
| 6 | +while being easy to understand for people without knowledge of the jsonapi standard. |
| 7 | + |
| 8 | +The JSON:API standard makes it easy for clients to fetch multiple resources in one call and understand the relations between them. |
| 9 | +Read more about it at [jsonapi.org](https://jsonapi.org/). |
| 10 | + |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +[Use Composer](http://getcomposer.org/) require to get the latest stable version: |
| 15 | + |
| 16 | +``` |
| 17 | +composer require alsvanzelf/jsonapi |
| 18 | +``` |
| 19 | + |
| 20 | +#### Upgrading from v1 |
| 21 | + |
| 22 | +If you used v1 of this library, see [UPGRADE_1_TO_2.md](/UPGRADE_1_TO_2.md) on how to upgrade. |
4 | 23 |
|
5 | | -It generates json output according to the [jsonapi.org](http://jsonapi.org/) standard, |
6 | | -but aims to be easy to understand for people without knowledge of the jsonapi standard. |
7 | 24 |
|
8 | 25 |
|
9 | 26 | ## Getting started |
10 | 27 |
|
11 | | -A small example: |
| 28 | +#### A small resource example |
12 | 29 |
|
13 | 30 | ```php |
14 | | -use alsvanzelf\jsonapi; |
15 | | - |
16 | | -$user = new stdClass(); |
17 | | -$user->id = 42; |
18 | | -$user->name = 'Zaphod Beeblebrox'; |
19 | | -$user->heads = 2; |
| 31 | +use alsvanzelf\jsonapi\ResourceDocument; |
20 | 32 |
|
21 | | -$jsonapi = new jsonapi\resource($type='user', $user->id); |
22 | | -$jsonapi->fill_data($user); |
23 | | -$jsonapi->send_response(); |
| 33 | +$document = new ResourceDocument($type='user', $id=42); |
| 34 | +$document->add('name', 'Zaphod Beeblebrox'); |
| 35 | +$document->add('heads', 2); |
| 36 | +$document->sendResponse(); |
24 | 37 | ``` |
25 | 38 |
|
26 | 39 | Which will result in: |
27 | 40 |
|
28 | 41 | ```json |
29 | 42 | { |
30 | | - "links": { |
31 | | - "self": "/examples/resource.php" |
32 | | - }, |
33 | | - "data": { |
34 | | - "type": "user", |
35 | | - "id": 42, |
36 | | - "attributes": { |
37 | | - "name": "Zaphod Beeblebrox", |
38 | | - "heads": 2 |
39 | | - }, |
40 | | - "links": { |
41 | | - "self": "/examples/resource.php" |
42 | | - } |
43 | | - } |
| 43 | + "jsonapi": { |
| 44 | + "version": "1.0" |
| 45 | + }, |
| 46 | + "data": { |
| 47 | + "type": "user", |
| 48 | + "id": "42", |
| 49 | + "attributes": { |
| 50 | + "name": "Zaphod Beeblebrox", |
| 51 | + "heads": 2 |
| 52 | + } |
| 53 | + } |
44 | 54 | } |
45 | 55 | ``` |
46 | 56 |
|
47 | | -For a collection response, data is an array of resources. |
48 | | -Errors can also be send as response, even automatically by exceptions. |
| 57 | +#### A collection of resources |
49 | 58 |
|
50 | | -Examples for all kind of responses are in the [/examples](/examples) directory. |
| 59 | +```php |
| 60 | +use alsvanzelf\jsonapi\CollectionDocument; |
51 | 61 |
|
| 62 | +$document = new CollectionDocument(); |
| 63 | +$document->add('user', 42, ['name' => 'Zaphod Beeblebrox']); |
| 64 | +$document->add('user', 1, ['name' => 'Ford Prefect']); |
| 65 | +$document->add('user', 2, ['name' => 'Arthur Dent']); |
| 66 | +$document->sendResponse(); |
| 67 | +``` |
52 | 68 |
|
53 | | -## Installation |
| 69 | +Which will result in: |
| 70 | + |
| 71 | +```json |
| 72 | +{ |
| 73 | + "jsonapi": { |
| 74 | + "version": "1.0" |
| 75 | + }, |
| 76 | + "data": [ |
| 77 | + { |
| 78 | + "type": "user", |
| 79 | + "id": "42", |
| 80 | + "attributes": { |
| 81 | + "name": "Zaphod Beeblebrox" |
| 82 | + } |
| 83 | + }, |
| 84 | + { |
| 85 | + "type": "user", |
| 86 | + "id": "1", |
| 87 | + "attributes": { |
| 88 | + "name": "Ford Prefect" |
| 89 | + } |
| 90 | + }, |
| 91 | + { |
| 92 | + "type": "user", |
| 93 | + "id": "2", |
| 94 | + "attributes": { |
| 95 | + "name": "Arthur Dent" |
| 96 | + } |
| 97 | + } |
| 98 | + ] |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +#### Turning an exception into jsonapi |
| 103 | + |
| 104 | +```php |
| 105 | +use alsvanzelf\jsonapi\ErrorsDocument; |
54 | 106 |
|
55 | | -[Use Composer](http://getcomposer.org/). And use require to get the latest stable version: |
| 107 | +$exception = new Exception('That is not valid', 422); |
56 | 108 |
|
| 109 | +$document = ErrorsDocument::fromException($exception); |
| 110 | +$document->sendResponse(); |
57 | 111 | ``` |
58 | | -composer require alsvanzelf/jsonapi |
| 112 | + |
| 113 | +Which will result in: |
| 114 | + |
| 115 | +```json |
| 116 | +{ |
| 117 | + "jsonapi": { |
| 118 | + "version": "1.0" |
| 119 | + }, |
| 120 | + "errors": [ |
| 121 | + { |
| 122 | + "status": "422", |
| 123 | + "code": "Exception", |
| 124 | + "meta": { |
| 125 | + "class": "Exception", |
| 126 | + "message": "That is not valid", |
| 127 | + "code": 422, |
| 128 | + "file": "README.md", |
| 129 | + "line": 107, |
| 130 | + "trace": [] |
| 131 | + } |
| 132 | + } |
| 133 | + ] |
| 134 | +} |
59 | 135 | ``` |
60 | 136 |
|
| 137 | +Examples for all kind of responses are in the [/examples](/examples) directory. |
61 | 138 |
|
62 | | -## To Do |
63 | 139 |
|
64 | | -Right now, this library handles all the basics: |
| 140 | +## Features |
65 | 141 |
|
66 | | -- generating single resources |
67 | | -- generating resource collections |
68 | | -- adding to-one and to-many relationships |
69 | | -- handling error responses |
70 | | -- sending out the json response with correct http headers |
| 142 | +This library supports [v1.0 of the JSON:API specification](https://jsonapi.org/format/1.0/). |
71 | 143 |
|
72 | | -Plus some handy tools: |
| 144 | +It has support for generating & sending documents with: |
73 | 145 |
|
74 | | -- easy turning thrown exceptions into jsonapi responses |
75 | | -- constants for easy setting http status codes |
76 | | -- sending out redirect locations |
| 146 | +- single resources |
| 147 | +- resource collections |
| 148 | +- to-one and to-many relationships |
| 149 | +- errors (easily turning exceptions into jsonapi output) |
77 | 150 |
|
78 | | -Plans for the [near](https://github.com/lode/jsonapi/labels/current%20focus) |
79 | | -and [later](https://github.com/lode/jsonapi/issues?utf8=%E2%9C%93&q=is%3Aopen+-label%3A%22current+focus%22+) future include: |
| 151 | +Plans for the future include: |
80 | 152 |
|
81 | | -- import a database array as a collection response ([#2](https://github.com/lode/jsonapi/issues/2)) |
82 | | -- handle creating, updating and deleting resources ([#5](https://github.com/lode/jsonapi/issues/5)) |
| 153 | +- support v1.1 of the specification ([#40](https://github.com/lode/jsonapi/pull/40)) |
| 154 | +- parse request options: sparse fields, sorting, pagination, filtering ([#44](https://github.com/lode/jsonapi/issues/44)) |
| 155 | +- parse requests for creating, updating and deleting resources and relationships ([#5](https://github.com/lode/jsonapi/issues/5)) |
83 | 156 |
|
84 | 157 |
|
85 | 158 | ## Contributing |
86 | 159 |
|
87 | | -Pull Requests or issues are welcome! |
| 160 | +[Pull Requests](https://github.com/lode/jsonapi/pulls) or [issues](https://github.com/lode/jsonapi/issues) are welcome! |
88 | 161 |
|
89 | 162 |
|
90 | 163 | ## Licence |
|
0 commit comments