Skip to content

Latest commit

 

History

History
64 lines (38 loc) · 1.5 KB

usage.md

File metadata and controls

64 lines (38 loc) · 1.5 KB

Usage

It is a good idea to have the JSON API definitions to hand:

Setup

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();

Simple use

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:

Class reference

Each class is explained in detail here:

Creating a custom object

If you need to make a custom object, then this is also possible:

Outside the JSON packet

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.

An example response in Laravel

Many people use the frameworks. There are "cheat sheet" examples in: