dev-master
9999999-dev https://github.com/cwbit/cakephp-jsonapiA set of libs for building standardized JSON responses in CakePHP 3.x REST APIs
MIT
The Requires
- php >=5.4
- cakephp/cakephp ~3.0
plugin api json cakephp rest jsonapi
A set of libs for building standardized JSON responses in CakePHP 3.x REST APIs
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)
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');
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)
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');
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)
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 } }
The JsonResponseTrait
exposes the following response functions, (*17)
respondWith($statusCode, $message, $data)
respondWithOK($message, $data)
- Status 200 - OKrespondWithCreated($message, $data)
- Status 201 - CREATEDrespondWithNoContent($message, $data)
- Status 204 - NO CONTENTrespondWithMovedPermanently($message, $data)
- Status 301 - MOVED PERMANENTLYrespondWithMovedTemporarily($message, $data)
- Status 302 - MOVED TEMPORARILYrespondWithSeeOther($message, $data)
- Status 303 - SEE OTHERrespondWithBadRequest($message, $data)
- Status 400 - BAD REQUESTrespondWithUnauthorized($message, $data)
- Status 401 - UNAUTHORIZEDrespondWithForbidden($message, $data)
- Status 403 - FORBIDDENrespondWithNotFound($message, $data)
- Status 404 - NOT FOUNDrespondWithMethodNotAllowed($message, $data)
- Status 405 - METHOD NOT ALLOWEDrespondWithConflict($message, $data)
- Status 409 - CONFLICTrespondWithInternalServerError($message, $data)
- Status 500 - INTERNAL SERVER ERRORIf 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)
A set of libs for building standardized JSON responses in CakePHP 3.x REST APIs
MIT
plugin api json cakephp rest jsonapi