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
**You do not need to implement all these methods** if extending this package's `Http\Controllers\JsonApiController`. The controller is configured to send a `501 Not Implemented` response for any missing methods.
97
97
@@ -129,13 +129,94 @@ Middleware makes it easy to register multiple routes (or even an entire API) tha
129
129
130
130
### Controller
131
131
132
+
JSON API support in controllers can be added by extending `CloudCreativity\JsonApi\Http\Controllers\JsonApiController`. This has a number of helper methods to assist in handling JSON API requests and sending responses.
133
+
134
+
Each of the following pieces of functionality are implemented using traits. So if you want to include any of the functionality in your own custom controllers, just include the relevant trait. The traits are in the same namespace as the `JsonApiController`.
135
+
132
136
#### Query Parameters
133
137
138
+
The `JsonApiController` will automatically check the query parameters before your controller action method is invoked. To define what query parameters your controller allows, set the properties that are defined in the `QueryCheckerTrait`.
139
+
140
+
This automatic checking means that by the time your controller action method is invoked, the request headers and query parameters have all been checked. Your action method can get the query parameters by calling `$this->getParameters()`.
141
+
142
+
If you want to disable automatic checking of query parameters before your controller methods are invoked, then set the `$autoCheckQueryParameters` property of your controller to `false`.
143
+
144
+
Note that if you are adding the trait to your own custom controller, you will need to call `$this->checkParameters()` to trigger the checking of parameters.
145
+
134
146
#### HTTP Content Body
135
147
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.
149
+
150
+
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
+
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.
152
+
153
+
These helper methods are implemented in the `DocumentDecoderTrait`.
154
+
136
155
#### Content Body Validation
137
156
138
-
### Responses
157
+
If desired, you can validate the body content as it is decoded. All of the content body getter methods accept an optional validator that is applied when the document content is decoded. All of methods for getting the request body content take an optional validator argument.
158
+
159
+
If the received content is a resource object, you can use the `$this->getResourceObjectValidator()` method. For example:
160
+
161
+
```php
162
+
class ArticlesController extends JsonApiController
163
+
{
164
+
// ...
165
+
166
+
public function update($id)
167
+
{
168
+
$validator = $this
169
+
->getResourceObjectValidator(
170
+
// the expected resource type
171
+
'articles',
172
+
// the expected id (use null for new resources)
173
+
$id,
174
+
// the rules for the attributes - uses the Laravel validation implementation.
175
+
[
176
+
'title' => 'string|max:250',
177
+
'content' => 'string',
178
+
'draft' => 'boolean',
179
+
],
180
+
// Laravel validation messages, if you need to customise them
181
+
[],
182
+
// whether the attributes member must be present in the resource object
183
+
true
184
+
);
185
+
186
+
$object = $this->getResourceObject($validator);
187
+
// ...
188
+
}
189
+
}
190
+
```
191
+
192
+
The validator returned by `$this->getResourceObjectValidator()` provides helper methods for also defining relationship validation rules.
193
+
194
+
For relationship endpoints, you can get a validator for the relationship provided using one of the following helper methods:
195
+
196
+
```php
197
+
class ArticlesController extends JsonApiController
0 commit comments