PHPackages                             cwbit/cakephp-jsonapi - 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. [API Development](/categories/api)
4. /
5. cwbit/cakephp-jsonapi

ActiveCakephp-plugin[API Development](/categories/api)

cwbit/cakephp-jsonapi
=====================

A set of libs for building standardized JSON responses in CakePHP 3.x REST APIs

55.0kPHP

Since Sep 1Pushed 10y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

cakephp-jsonapi
===============

[](#cakephp-jsonapi)

Set of libraries for building standardized JSON responses in CakePHP 3.x REST APIs

***WHY?***I needed a consistent way to get REST reponses back from my API controllers. Additionally, there's a few basic setup steps that a Controller needs in order to properly handle JSON request/responses.

***HOW?***Just add the `JsonControllerTrait` to turn any Controller into a JSON-friendly controller. Add the `JsonResponseTrait` to expose a number of response functions (detailed below).

Requirements
------------

[](#requirements)

- PHP 5.4+
- [CakePHP 3.x](http://cakephp.org)

TOC
---

[](#toc)

1. Plugin Installation
2. Usage
3. Response Methods
4. Contributing

### Plugin Installation

[](#plugin-installation)

1. Composer Install
2. Manual Install
3. Loading the plugin in your app
4. Setting up the namespace / autoloader

#### Composer Install

[](#composer-install)

This plugin is on Packagist which means it can be easily installed with Composer.

```
composer require cwbit/cakephp-jsonapi:dev-master

```

Then simply load the plugin normally in your `config/bootstrap.php` file

```
# in ../config/bootstrap.php - right after Plugin::load('Migrations') is fine!
Plugin::load('JsonApi');
```

#### Manual Install

[](#manual-install)

You can also manually load this plugin in your App.

⚠️ Installing the plugin without the use of Composer is not officially supported. You do so at your own risk.

##### loading the plugin in your app

[](#loading-the-plugin-in-your-app)

Add the source code in this project into `plugins/JsonApi`

Then configure your App to actually load this plugin

```
# in ../config/bootstrap.php
Plugin::load('JsonApi');
```

##### setting up the namespace / autoloader

[](#setting-up-the-namespace--autoloader)

Tell the autoloader where to find your namespace in your `composer.json` file

```
	(..)
    "autoload": {
        "psr-4": {
           (..)
            "JsonApi\\": "./plugins/JsonApi/src"
        }
    },
    (..)
```

Then you need to issue the following command on the commandline

```
	php composer.phar dumpautoload

```

If you are unable to get composer autoloading to work, add `'autoload' => true` line in your `bootstrap.php` `Plugin::load(..)` command (see loading section)

Usage
-----

[](#usage)

The easiest way to get this set up is to simply add an Api namespace to your App. This way you can control exactly what your API does.

To set up an Api namespace, add the following to your `routes.php`

```
# in routes.php
Router::prefix('api', function ($routes) {
    $routes->fallbacks('InflectedRoute');
});
```

Then, create an Api controller inside `src/Controller/Api`

```
