It is a good idea to have the JSON API definitions to hand:
The class can be included by whatever method is appropriate:
// Example PSR-4 include
use Floor9design\JsonApiFormatter\Models\JsonApiFormatter;
The object can then be instantiated:
// Example PSR-4 include
use Floor9design\JsonApiFormatter\Models\JsonApiFormatter;
$json_api_response = new JsonApiFormatter();
Most use cases fit the included simple-use functions. These are described as follows:
If you prefer to learn by example, there is also a tutorial that builds several responses:
Each class is explained in detail here:
If you need to make a custom object, then this is also possible:
The JSON API requires a specific Content-Type
header, which can be loaded by getContentType()
:
use Floor9design\JsonApiFormatter\Models\JsonApiFormatter;
$json_api_response = new JsonApiFormatter();
$content_type = $json_api_response->getContentType();
// application/vnd.api+json
The dataResourceResponse()
returns a JSON API compliant json string.
Many people use the frameworks. There are "cheat sheet" examples in: