2017 © Pedro Peláez
 

cakephp-plugin cake-php-api-manager

An extensive plugin to handle common API functions for CakePHP.

image

asugai/cake-php-api-manager

An extensive plugin to handle common API functions for CakePHP.

  • Thursday, July 21, 2016
  • by asugai
  • Repository
  • 0 Watchers
  • 0 Stars
  • 1,162 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

CakePHP API Manager

An API abstraction layer for CakePHP that includes API level exceptions, (*1)

Background

Allows for an easy way to create a request data layer to normalize incoming data and includes exception classes for common error cases such as login, save and missing params., (*2)

Requirements

  • PHP >= 5.3
  • CakePHP 2.x
  • Basic knowledge of Exceptions in CakePHP

Installation

[Manual], (*3)

  • Download this: http://github.com/asugai/CakePHP-API-Manager/zipball/master
  • Unzip that download.
  • Copy the resulting folder to app/Plugin
  • Rename the folder you just copied to ApiManager

[GIT Submodule], (*4)

In your app directory type:, (*5)

git submodule add git://github.com/asugai/CakePHP-API-Manager.git Plugin/ApiManager
git submodule update --init

[GIT Clone], (*6)

In your app directory type, (*7)

git clone git://github.com/asugai/CakePHP-API-Manager.git Plugin/ApiManager

Enable plugin

Enable the plugin your app/Config/bootstrap.php file:, (*8)

CakePlugin::load('ApiManager');

If you are already using CakePlugin::loadAll();, then this is not necessary., (*9)

Usage

Setup ApiManager

Setup the autoloader if you are using composer in /app/Config/bootstrap.php:, (*10)

// Load composer autoload.
require APP . '/Vendor/autoload.php';

// Remove and re-prepend CakePHP's autoloader as composer thinks it is the most important.
// See https://github.com/composer/composer/commit/c80cb76b9b5082ecc3e5b53b1050f76bb27b127b
spl_autoload_unregister(array('App', 'load'));
spl_autoload_register(array('App', 'load'), true, true);

Optional: Edit /app/Config/bootstrap.php file and add ApiManager keys:, (*11)

Configure::write('ApiManager.softErrors', false);
Configure::write('ApiManager.log', false);
Configure::write('ApiManager.legacy', false);
Configure::write('ApiManager.objectifyExclude', []);

Setup AppController to extend ApiController:, (*12)

class AppController extends ApiController {
    ...
}

Create request models using ApiRequest as base, example created in /app/Request/User/UserLoginRequest.php:, (*13)

class UserLoginRequest extends ApiRequest {
    const EMAIL = 'email';
    const PASSWORD = 'password';

    protected $map = [
        self::EMAIL,
        self::PASSWORD
    ];

    protected $mandatory = [
        self::EMAIL => 'Email',
        self::PASSWORD => 'Password'
    ];

    protected function afterCheckCallback() {
        parent::afterCheckCallback();
    }
}

Create normalization routing in controller:, (*14)

App::uses('UserLoginRequest', 'Request/User');

class UsersController extends AppController
{
    ...

    public function api_login()
    {
        $this->api_DefaultFlow($this->User, 'api_login', 'UserLoginRequest');
    }

    ...
}

Use the normalized object in the model function:, (*15)

class User extends AppModel
{
    ...

    public function api_login(\UserLoginRequest $request)
    {
        if (!$user = $this->findByEmailAndPassword(
            $request->email, 
            $request->password
        )) {
            throw new ApiLoginException();
        }

        return $user;
    }
}

Todo

  • Comments!

Aknowledgements

The basic layout of this of this README was taken from https://github.com/dkullmann/CakePHP-Elastic-Search-DataSource, (*16)

License

Copyright (c) 2014 Andre Sugai, (*17)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:, (*18)

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software., (*19)

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE., (*20)

The Versions

25/02 2015

dev-master

9999999-dev http://github.com/asugai/cake-php-api-manager

A simple lib to handle common API functions for CakePHP.

  Sources   Download

MIT

The Requires

 

api cakephp exception