2017 © Pedro Peláez
 

cakephp-plugin cakephp-jsonapi

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

image

cwbit/cakephp-jsonapi

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

  • Tuesday, September 1, 2015
  • by cwbit
  • Repository
  • 2 Watchers
  • 5 Stars
  • 1,455 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 4 % Grown

The README.md

cakephp-jsonapi

Set of libraries for building standardized JSON responses in CakePHP 3.x REST APIs, (*1)

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., (*2)

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)., (*3)

Requirements

TOC

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

Plugin Installation

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

Composer Install

This plugin is on Packagist which means it can be easily installed with Composer., (*4)

composer require cwbit/cakephp-jsonapi:dev-master

Then simply load the plugin normally in your config/bootstrap.php file, (*5)

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

Manual Install

You can also manually load this plugin in your App., (*6)

:warning: Installing the plugin without the use of Composer is not officially supported. You do so at your own risk., (*7)

loading the plugin in your app

Add the source code in this project into plugins/JsonApi, (*8)

Then configure your App to actually load this plugin, (*9)

# in ../config/bootstrap.php
Plugin::load('JsonApi');
setting up the namespace / autoloader

Tell the autoloader where to find your namespace in your composer.json file, (*10)

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

Then you need to issue the following command on the commandline, (*11)

    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), (*12)

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., (*13)

To set up an Api namespace, add the following to your routes.php, (*14)

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

Then, create an Api controller inside src/Controller/Api, (*15)

<?php

namespace App\Controller\Api;

use App\Controller\AppController as Controller;
use JsonApi\Controller\JsonControllerTrait;
use JsonApi\Controller\JsonResponseTrait;

class AppController extends Controller
{

    use JsonControllerTrait;
    use JsonResponseTrait;

}

That's it! Now any of your controllers inside the Api namespace can automatically accept and respond to JSON calls, (*16)

<?php
namespace App\Controller\Api;

use App\Controller\Api\AppController;

class FoosController extends AppController
{
    public function view()
    {
        # if no ID passed, respond with error 400
        if (!$this->request->data('id')) :
            return $this->respondWithBadRequest('Foo id is required');
        endif;

        # .. etc
    }
}

Response Methods

The JsonResponseTrait exposes the following response functions, (*17)

  • respondWith($statusCode, $message, $data)
  • SUCCESS
  • respondWithOK($message, $data) - Status 200 - OK
  • respondWithCreated($message, $data) - Status 201 - CREATED
  • respondWithNoContent($message, $data) - Status 204 - NO CONTENT
  • REDIRECTION
  • respondWithMovedPermanently($message, $data) - Status 301 - MOVED PERMANENTLY
  • respondWithMovedTemporarily($message, $data) - Status 302 - MOVED TEMPORARILY
  • respondWithSeeOther($message, $data) - Status 303 - SEE OTHER
  • CLIENT ERROR
  • respondWithBadRequest($message, $data) - Status 400 - BAD REQUEST
  • respondWithUnauthorized($message, $data) - Status 401 - UNAUTHORIZED
  • respondWithForbidden($message, $data) - Status 403 - FORBIDDEN
  • respondWithNotFound($message, $data) - Status 404 - NOT FOUND
  • respondWithMethodNotAllowed($message, $data) - Status 405 - METHOD NOT ALLOWED
  • respondWithConflict($message, $data) - Status 409 - CONFLICT
  • SERVER ERROR
  • respondWithInternalServerError($message, $data) - Status 500 - INTERNAL SERVER ERROR

Contributing

If you'd like to contribute, please fork this repo, and make a PR of your changes! Any PRs with accompanying tests are much more likely to be accepted., (*18)

The Versions

01/09 2015

dev-master

9999999-dev https://github.com/cwbit/cakephp-jsonapi

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

  Sources   Download

MIT

The Requires

 

plugin api json cakephp rest jsonapi