PHPackages                             josbeir/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. josbeir/cakephp-json-api

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

josbeir/cakephp-json-api
========================

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

0.1.0(10y ago)251.4k7MITPHPPHP &gt;=5.4.16

Since Feb 20Pushed 8y ago4 watchersCompare

[ Source](https://github.com/josbeir/cakephp-json-api)[ Packagist](https://packagist.org/packages/josbeir/cakephp-json-api)[ RSS](/packages/josbeir-cakephp-json-api/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

### !!! THIS PLUGIN IS NOW DEPRECTATED, PLEASE REFER TO [crud-json-api](https://github.com/FriendsOfCake/crud-json-api) for a better alternative

[](#-this-plugin-is-now-deprectated-please-refer-to-crud-json-api-for-a-better-alternative)

json-api plugin for CakePHP 3
=============================

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

[![Latest Stable Version](https://camo.githubusercontent.com/0509518b48da2558e7a1faae789302c6b4249f7e7e13097916d7ef134b6c8407/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73626569722f63616b657068702d6a736f6e2d6170692f762f737461626c65)](https://packagist.org/packages/josbeir/cakephp-json-api)[![Total Downloads](https://camo.githubusercontent.com/d76f544c2672356165755002736c491fd76813c05dfeb886190d361a9550f753/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73626569722f63616b657068702d6a736f6e2d6170692f646f776e6c6f616473)](https://packagist.org/packages/josbeir/cakephp-json-api)[![Latest Unstable Version](https://camo.githubusercontent.com/4594159fb530626893674331406407df5d8777264c841550a9327fe47119ea05/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73626569722f63616b657068702d6a736f6e2d6170692f762f756e737461626c65)](https://packagist.org/packages/josbeir/cakephp-json-api)[![License](https://camo.githubusercontent.com/3571308a78b82ec3e5474dc0f22fce948101737393192753e7aced1983cb2f0b/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73626569722f63616b657068702d6a736f6e2d6170692f6c6963656e7365)](https://packagist.org/packages/josbeir/cakephp-json-api)[![codecov.io](https://camo.githubusercontent.com/551b08b9123d414244240ea1a3aa1a6334aa419e7b6234b1e2573b90b9fe647e/68747470733a2f2f636f6465636f762e696f2f6769746875622f6a6f73626569722f63616b657068702d6a736f6e2d6170692f636f7665726167652e7376673f6272616e63683d6d6173746572)](https://codecov.io/github/josbeir/cakephp-json-api?branch=master)[![Build Status](https://camo.githubusercontent.com/594a2f037e2a0e2ade1d7db6b3b64f7b921138b9c1c4ee1d3656e3580ec27f5d/68747470733a2f2f7472617669732d63692e6f72672f6a6f73626569722f63616b657068702d6a736f6e2d6170692e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/josbeir/cakephp-json-api)

[![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.

Disclaimer
----------

[](#disclaimer)

Very much a work in progress. My goal is make it as feature complete as possible but contributions are welcome. Features are added on an occasional basis.

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 josbeir/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)

```
