You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+53-4Lines changed: 53 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,17 @@ For full information on the spec, plus examples, see http://jsonapi.org
12
12
13
13
## Features
14
14
15
-
@todo
15
+
* Encoding of JSON API responses using the `neomerx/json-api` package.
16
+
* Start JSON API support on route groups through a single piece of middleware, which automatically checks request headers and loads JSON API query parameters.
17
+
* Registration of JSON API defined resource end-points via a simple route helper.
18
+
* Define supported JSON API extensions via middleware (on route groups or individual controllers).
19
+
* A JSON API controller providers helpers to:
20
+
- Automatically check request query parameters.
21
+
- Decode request body content into objects with a standard, fluent, interface. Makes handling content easier.
22
+
- Validate request body content as it is being decoded, including using Laravel validates on resource object attributes.
23
+
- Helpers for sending common JSON API responses, that automatically include requested encoding parameters, the encoded media type plus registered supported extensions.
24
+
* Rendering of exceptions into JSON API error responses, plus easy sending of error responses via throwable JSON API error objects.
25
+
* Configuration settings - including schemas, encoders, decoders and exception rendering - all defined in a configuration file in your Laravel application.
16
26
17
27
## Installation
18
28
@@ -60,7 +70,7 @@ Route::group(['middleware' => M::JSON_API], function () {
60
70
});
61
71
```
62
72
63
-
If every route in your application is a JSON API endpoint, then you can set the `C::IS_GLOBAL` option to true. This will install the same piece of middleware on the HTTP Kernel, so that it is invoked for every request.
73
+
If every route in your application is a JSON API endpoint, then you can set the `C::IS_GLOBAL` option to `true`. This will install the same piece of middleware on the HTTP Kernel, so that it is invoked for every request.
64
74
65
75
#### Defining Endpoints
66
76
@@ -89,7 +99,7 @@ Per resource type, the following endpoints will be registered (using the `articl
89
99
| /articles/:id/relationships/author | GET |`readAuthorRelationship($id)`|
@@ -145,7 +155,7 @@ Note that if you are adding the trait to your own custom controller, you will ne
145
155
146
156
#### HTTP Content Body
147
157
148
-
To decode the request content body with the decoder that matched the request `Content-Type` header, then call `$this->getContentBody()`. If no decoder has matched the request header, calling this method will result in a `400 Bad Request` response to the client.
158
+
To decode the request content body with the decoder that matched the request `Content-Type` header, then call `$this->getContentBody()`.
149
159
150
160
If you want to use a `CloudCreativity\JsonApi\Contracts\Object\DocumentInterface` object to handle the request content in your controller action, use `$this->getDocumentObject()`. This method ensures the decoder has returned a `DocumentInterface` object, or if it has returned a `stdClass` object it will be cast to a `DocumentInterface` object.
151
161
Shorthands are also provided if you are expecting the document to contain a resource object in its data member, or if the provided document represents a relationship. Use `$this->getResourceObject()` and `$this->getRelationshipObject()` respectively.
@@ -218,6 +228,45 @@ These helper methods are provided by the `DocumentValidatorTrait`, which uses th
218
228
219
229
#### Responses
220
230
231
+
The class `CloudCreativity\JsonApi\Http\Responses\ResponsesHelper` provides a number of methods for constructing JSON API responses. This helper automatically uses request encoding parameters, the matched encoding media type plus any registered supported extensions.
232
+
233
+
If you have extended the `JsonApiController`, you can use this helper by calling `$this->reply()` then the method for the type of response you want to send. For example, to send a content response:
234
+
235
+
```php
236
+
class ArticlesController extends JsonApiController
237
+
{
238
+
// ...
239
+
240
+
public function read($id)
241
+
{
242
+
$article = Article::find($id);
243
+
244
+
if (!$article) {
245
+
$this->notFound();
246
+
}
247
+
248
+
return $this
249
+
->reply()
250
+
->content($article);
251
+
}
252
+
}
253
+
```
254
+
255
+
The available helpers are:
256
+
257
+
| Method | Detail |
258
+
| :----- | :----- |
259
+
|`statusCode`| Send a status code only reply |
260
+
|`noContent`| Send a no content reply (204) |
261
+
|`meta`| Send meta only |
262
+
|`content`| Send content (resource object, null, or collection) |
263
+
|`created`| Send a resource created response (encoded object, location header and 201 status |
264
+
|`relationship`| Send a relationship response (encoded identifier, null, or collection of identifiers) |
265
+
266
+
See the class for the parameters that each helper method accepts.
267
+
268
+
The reply method is added to the controller via the `ReplyTrait`.
269
+
221
270
### Exception Handling
222
271
223
272
To add JSON API support to your application's Exception Handler, add the `Exceptions\HandlerTrait` to your `App\Exceptions\Handler` instance. Then, in your `render()` method you can do the following:
0 commit comments