All Projects → sprintcube → cakephp-rest

sprintcube / cakephp-rest

Licence: MIT License
CakePHP REST Plugin - Easily build REST API endpoints in your CakePHP application.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to cakephp-rest

cakephp-ajax
AJAX for CakePHP: A plugin to ease handling AJAX requests.
Stars: ✭ 55 (+139.13%)
Mutual labels:  cakephp, cakephp-plugin
mixerapi
A CakePHP Plugin for RESTful API Development [READ-ONLY]
Stars: ✭ 26 (+13.04%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-i18n
A CakePHP plugin with I18n related tools.
Stars: ✭ 40 (+73.91%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-error-email
ErrorEmail Plugin for CakePHP3.x
Stars: ✭ 16 (-30.43%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-api-pagination
📑 CakePHP 4 plugin that injects pagination information into API responses.
Stars: ✭ 39 (+69.57%)
Mutual labels:  cakephp, cakephp-plugin
plum search
Plum Search plugin for CakePHP
Stars: ✭ 20 (-13.04%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-glide
CakePHP plugin for using Glide image manipulation library
Stars: ✭ 34 (+47.83%)
Mutual labels:  cakephp, cakephp-plugin
Cakephp Csvview
CakePHP: A view class for generating CSV
Stars: ✭ 174 (+656.52%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-translate
A CakePHP plugin to manage translations of your static content the easy way via web backend.
Stars: ✭ 18 (-21.74%)
Mutual labels:  cakephp, cakephp-plugin
Orderly
Default ordering for your CakePHP tables
Stars: ✭ 21 (-8.7%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-sequence
CakePHP plugin for maintaining a contiguous sequence of records
Stars: ✭ 41 (+78.26%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-mailgun
Mailgun plugin for CakePHP 3
Stars: ✭ 23 (+0%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-feed
CakePHP Plugin with RssView to create RSS feeds.
Stars: ✭ 13 (-43.48%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-shim
CakePHP plugin to "shim" functionality up and down for major versions of the framework.
Stars: ✭ 37 (+60.87%)
Mutual labels:  cakephp, cakephp-plugin
auth
Auth objects for CakePHP
Stars: ✭ 28 (+21.74%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-swagger-bake
Automatically generate OpenAPI, Swagger, and Redoc documentation from your existing CakePHP code.
Stars: ✭ 48 (+108.7%)
Mutual labels:  cakephp, cakephp-plugin
Search
CakePHP: Easy model searching
Stars: ✭ 153 (+565.22%)
Mutual labels:  cakephp, cakephp-plugin
Cakephp Jwt Auth
A CakePHP plugin for authenticating using JSON Web Tokens
Stars: ✭ 153 (+565.22%)
Mutual labels:  cakephp, cakephp-plugin
elastic-search
Elastic search datasource for CakePHP
Stars: ✭ 85 (+269.57%)
Mutual labels:  cakephp, cakephp-plugin
asset-mix
Provides helpers functions for CakePHP to use Laravel Mix.
Stars: ✭ 27 (+17.39%)
Mutual labels:  cakephp, cakephp-plugin

Rest Plugin for CakePHP

Build Status GitHub license Total Downloads Latest Stable Version

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

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require sprintcube/cakephp-rest

After installation, Load the plugin

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

Or, you can load the plugin using the shell command

$ 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,

$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,

<?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.

{
    "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.

Response Format

This plugin returns the response in the following format.

{
    "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.

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.

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.

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,

$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.

Authorization: Bearer [token]

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

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.

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

/**
 * 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.

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,

<?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.

/**
 * 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.

Reporting Issues

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].