PHP code example of phantomwatson / cakephp-json-api

1. Go to this page and download the library: Download phantomwatson/cakephp-json-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phantomwatson / cakephp-json-api example snippets


Plugin::load('JsonApi');

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

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('_


namespace TestApp\View\Schema;

use JsonApi\View\Schema\EntitySchema;

class AuthorSchema extends EntitySchema
{
    public function getId($entity)
    {
        return $entity->get('id');
    }

    public function getAttributes($entity)
    {
        return [
            'title' => $entity->title,
            'body' => $entity->body,
            'published' => $entity->published,
            'helper_link' => $this->Url->build(['action' => 'view']) // view helper
        ];
    }

    public function getRelationships($entity, array $

$this->RequestHandler->config('inputTypeMap.jsonapi', ['json_decode', true]);

Router::scope('/api', function($routes) {
	$routes->resources('Articles', function($routes) {
		$routes->resources('Authors');
	});
});
$this->getView()