PHPackages                             phantomwatson/cakephp-json-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. phantomwatson/cakephp-json-api

ActiveCakephp-plugin[Parsing &amp; Serialization](/categories/parsing)

phantomwatson/cakephp-json-api
==============================

json-api specification plugin for CakePHP - http://jsonapi.org/

2.0.0(1y ago)0622MITPHPPHP &gt;=8.0

Since Feb 20Pushed 1y agoCompare

[ Source](https://github.com/PhantomWatson/cakephp-json-api)[ Packagist](https://packagist.org/packages/phantomwatson/cakephp-json-api)[ RSS](/packages/phantomwatson-cakephp-json-api/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (4)Versions (11)Used By (0)

json-api plugin for CakePHP 5
=============================

[](#json-api-plugin-for-cakephp-5)

[![json:api](https://camo.githubusercontent.com/5a13bdb171c3e1f6fc251a70f751b4b1957b598dd08dcf265ca7867fc1fef4b1/687474703a2f2f6a736f6e6170692e6f72672f696d616765732f6a736f6e6170692e706e67)](https://camo.githubusercontent.com/5a13bdb171c3e1f6fc251a70f751b4b1957b598dd08dcf265ca7867fc1fef4b1/687474703a2f2f6a736f6e6170692e6f72672f696d616765732f6a736f6e6170692e706e67)

This plugin implements [neomerx/json-api](https://github.com/neomerx/json-api) as a View class for cakephp3.

> JSON API is a specification for how a client should request that resources be fetched or modified, and how a server should respond to those requests.
>
> JSON API is designed to minimize both the number of requests and the amount of data transmitted between clients and servers. This efficiency is achieved without compromising readability, flexibility, or discoverability.
>
> JSON API requires use of the JSON API media type (application/vnd.api+json) for exchanging data.

Installation
------------

[](#installation)

You can install this plugin into your CakePHP application using [composer](http://getcomposer.org).

The recommended way to install composer packages is:

```
composer require phantomwatson/cakephp-json-api:dev-master

```

Usage
-----

[](#usage)

This plugin works by using [neomerx/json-api](https://github.com/neomerx/json-api) php module at its core, my advice is to read up on the docs before proceeding.

Load the plugin by adding it to your bootstrap.php

```
Plugin::load('JsonApi');
```

or activate it using the cake shell

```
$ bin/cake plugin load JsonApi

```

Then tell your controller to use the JsonApi view

```
$this->viewBuilder()->className('JsonApi.JsonApi');
```

The following view variables can be assigned in your controller

VariableDescription`_serialize`this holds the actual data to pass to the encoder instance, can be an array of entities, a single entity.`_url`the base url of the api endpoint`_entities`**required** A list of entities that are going to be mapped to Schemas`_include`an array of hash paths what should be in the [included](http://jsonapi.org/format/#fetching-includes) section of the response. `[ 'posts.author', 'comments' ]``_fieldsets`A hash path of fields should be in the resultset `[ 'sites'  => ['name'], 'people' => ['first_name'] ]``_meta`meta data to add to the document`_links`links to add to the document this should be an array of `Neomerx\JsonApi\Schema\Link` objects.#### Example

[](#example)

```
public function initialize()
{
	$this->viewBuilder()->className('JsonApi.JsonApi');

	$this->set('_entities', [
		'Article',
		'Author'
	]);

	$this->set('_url', Router::url('/api', true));
	$this->set('_meta', ['some' => 'global metadata']);
	$this->set('_links', [ // uses Neomerx\JsonApi\Schema\Link
		Link::FIRST => new Link('/authors?page=1'),
		Link::LAST => new Link('/authors?page=9', [
			'meta' => 'data'
		])
	]);
}

public function index()
{
	$articles = $this->Articles->find()
		->all();

	$this->set(compact('articles'));
	$this->set('_serialize', true);

	// optional parameters
	$this->set('_include', [ 'articles', 'articles.comments' ]);
	$this->set('_fieldsets', [ 'articles' => [ 'title' ] ]);
}
```

Schemas
-------

[](#schemas)

Entities assigned in `_entities` are mapped to the `EntitySchema` base class. This class extends `Neomerx\JsonApi\Schema\SchemaProvider`.

It is **recommended** that you create a schema class for each entity you defined by extending the EntitySchema class. Example: if you have an entity in `Model\Entity\Author` then create a schema class in `View\Schema\AuthorSchema`

Think of the Schema class as a template that represents an Entity.

Because of this it is possible access the current view object along with Request and helpers. `$this->getView()` can be called inside the schema if you need it.

### Schema example

[](#schema-example)

Example App\\View\\Schema\\AuthorSchema.php (maps to App\\Model\\Entity\\Author)

```
