2017 © Pedro Peláez
 

cakephp-plugin cakephp-rest

Rest API plugin for CakePHP 3

image

pnglabz/cakephp-rest

Rest API plugin for CakePHP 3

  • Tuesday, July 3, 2018
  • by narendravaghela
  • Repository
  • 2 Watchers
  • 4 Stars
  • 15 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 5 Versions
  • 650 % Grown

The README.md

Rest Plugin for CakePHP

Build Status GitHub license Total Downloads Latest Stable Version, (*1)

This plugin simplifies the REST API development for your CakePHP 3 application. It simply converts the output of your controller into a JSON response., (*2)

Installation

You can install this plugin into your CakePHP application using composer., (*3)

The recommended way to install composer packages is:, (*4)

composer require sprintcube/cakephp-rest

After installation, Load the plugin, (*5)

Plugin::load('Rest', ['bootstrap' => true]);

Or, you can load the plugin using the shell command, (*6)

$ bin/cake plugin load -b Rest

Usage

No major change requrires in the way you code in your CakePHP application. Simply, just add one parameter to your route configuration isRest like,, (*7)

$routes->connect('/foo/bar', ['controller' => 'Foo', 'action' => 'bar', 'isRest' => true]);

And extend your controller to RestController and everything will be handled by the plugin itself. For example,, (*8)

<?php

namespace App\Controller;

use Rest\Controller\RestController;

/**
 * Foo Controller
 *
 */
class FooController extends RestController
{

    /**
     * bar method
     *
     * @return Response|void
     */
    public function bar()
    {
        $bar = [
            'falanu' => [
                'dhikanu',
                'tamburo'
            ]
        ];

        $this->set(compact('bar'));
    }
}

And that's it. You will see the response as below., (*9)

{
    "status": "OK",
    "result": {
        "bar": {
            "falanu": [
                "dhikanu",
                "tamburo"
            ]
        }
    }
}

Doesn't it too simple? Whatever viewVars you set from your controller's action using set() method, will be converted into JSON response., (*10)

Response Format

This plugin returns the response in the following format., (*11)

{
    "status": "OK",
    "result": {
        ...
    }
}

The status key may contain OK or NOK based on your response code. For all successful responses, the code will be 200 and the value of this key will be OK., (*12)

In case of error or exception, the value of status will become NOK. Also, based on your application's debug setting, it will contain the exception and trace data., (*13)

The result key contains the actual response. It holds all the variables set from your controller. This key will not be available in case of error/exception., (*14)

Require Authentication??

This plugin also provides an option to authenticate request using JWT. Simply, just add one more parameter to your route configuration requireAuthorization like,, (*15)

$routes->connect('/foo/bar', ['controller' => 'Foo', 'action' => 'bar', 'isRest' => true, 'requireAuthorization' => true]);

Now, the plugin will check for the JWT token in the request in form of a header, query parameter or post data. If you want to pass the token in the header, use the following format., (*16)

Authorization: Bearer [token]

And for query parameter or post data, use token parameter and set the token as a value of the parameter., (*17)

Generate a token

If you require the authentication in API, you first must grant the token to the user who is making the API request. In general, when a user logs in, the response should contain the token for all next requests., (*18)

To generate a token, use the method from Utility class of the plugin: JwtToken::generate()., (*19)

/**
 * login method
 *
 * @return Response|void
 */
public function login()
{
    // you user authentication code will go here, you can compare the user with the database or whatever

    $payload = [
        'id' => "Your User's ID",
        'other' => "Some other data"
    ];

    $token = \Rest\Utility\JwtToken::generate($payload);

    $this->set(compact('token'));
}

And it will return the token in response. So, in next API calls, a user can use that token for authorization. You can add whatever data is required in your payload., (*20)

By default, the plugin uses the predefined key and algorithm to generate JWT token. You can update this configuration by creating config/rest.php file. The content of this configuration file will be as following,, (*21)

<?php
return [
    'Rest' => [
        'jwt' => [
            'key' => 'PUT YOUR KEY HERE', // it should contain alphanumeric string with symbols
            'algorithm' => 'HS256' // See https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
        ]
    ]
];

Access token data

If there is a valid token available in the request, you can access it in your controller using the token and payload properties., (*22)

/**
 * view method
 *
 * @return Response|void
 */
public function view()
{
    $token = $this->token;

    $payload = $this->payload;

    // your action logic...
}

These properties are also available in your controller's beforeFilter method, so you can put additional authentication logic there., (*23)

Reporting Issues

If you have a problem with this plugin or found any bug, please open an issue on GitHub., (*24)

The Versions

03/07 2018

dev-master

9999999-dev

Rest API plugin for CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

api rest api cakephp rest cakephp rest cakephp rest api

03/07 2018

v1.0.2

1.0.2.0

Rest API plugin for CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

api rest api cakephp rest cakephp rest cakephp rest api

03/07 2018

dev-patch-1

dev-patch-1

Rest API plugin for CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

api rest api cakephp rest cakephp rest cakephp rest api

04/06 2018

v1.0.1

1.0.1.0

Rest API plugin for CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

api rest api cakephp rest cakephp rest cakephp rest api

12/04 2018

v1.0.0

1.0.0.0

Rest API plugin for CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

api rest api cakephp rest cakephp rest cakephp rest api