Download the PHP package josbeir/cakephp-json-api without Composer
On this page you can find all versions of the php package josbeir/cakephp-json-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package cakephp-json-api
!!! THIS PLUGIN IS NOW DEPRECTATED, PLEASE REFER TO crud-json-api for a better alternative
json-api plugin for CakePHP 3
This plugin implements 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
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
You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
Usage
This plugin works by using 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
or activate it using the cake shell
Then tell your controller to use the JsonApi view
The following view variables can be assigned in your controller
Variable | Description |
---|---|
_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 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
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. can be called inside the schema if you need it.
Schema example
Example App\View\Schema\AuthorSchema.php (maps to App\Model\Entity\Author)
Request handling and routing
This plugin does not handle this for you but can be easily added to your application using cake's RequestHandler component which has support for the json-api Content-Type.
For instance, if you want to automatically decode incoming json-api (application/vnd.api+json) data you can tell RequestHandler to automaticaly handle it.
RESTfull routing can also be achieved by creating resource routes.