PHPackages                             memorodmx/cakephp-rest-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. memorodmx/cakephp-rest-api

ActiveCakephp-plugin[HTTP &amp; Networking](/categories/http)

memorodmx/cakephp-rest-api
==========================

CakePHP 3 plugin to provide basic support for building REST API services

v1.2(5y ago)015MITPHPPHP &gt;=5.6.0

Since Oct 23Pushed 5y agoCompare

[ Source](https://github.com/MemoRodMx/cakephp-rest-api)[ Packagist](https://packagist.org/packages/memorodmx/cakephp-rest-api)[ Docs](https://github.com/cakephp/debug_kit)[ RSS](/packages/memorodmx-cakephp-rest-api/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (2)Dependencies (4)Versions (7)Used By (0)

RestApi plugin for CakePHP 3 and CakePHP 4
==========================================

[](#restapi-plugin-for-cakephp-3-and-cakephp-4)

[![Build Status](https://camo.githubusercontent.com/326aa84a663d5ae143b9bd5ac1d5ff96430a7f121c595b351aefa41c0ffafac2/68747470733a2f2f7472617669732d63692e6f72672f6d756c7469646f74732f63616b657068702d726573742d6170692e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/multidots/cakephp-rest-api)[![GitHub license](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](https://raw.githubusercontent.com/multidots/cakephp-rest-api/master/LICENSE)[![Total Downloads](https://camo.githubusercontent.com/c9932c74d9afe6355526a22476b1bede84d8f814260569948d4f74eed7e05977/68747470733a2f2f706f7365722e707567782e6f72672f6d756c7469646f74732f63616b657068702d726573742d6170692f646f776e6c6f616473)](https://packagist.org/packages/multidots/cakephp-rest-api)[![Latest Stable Version](https://camo.githubusercontent.com/9bf22ed8128a51d8c5a59bd08204228a4760ffdc2e353d36b6aa5c95acba2734/68747470733a2f2f706f7365722e707567782e6f72672f6d756c7469646f74732f63616b657068702d726573742d6170692f762f737461626c65)](https://packagist.org/packages/multidots/cakephp-rest-api)

This plugin provides basic support for building REST API services in your CakePHP 3 application. Read a detailed guide on how to implement this here - [CakePHP: Build REST APIs with RestApi plugin](http://blog.narendravaghela.com/cakephp-build-rest-apis-with-restapi-plugin-part-1/)

Requirements
------------

[](#requirements)

This plugin has the following requirements:

- CakePHP 3.0.0 / 4.0.0 or greater.
- PHP 5.4.16 or greater.

Installation
------------

[](#installation)

You can install this plugin into your CakePHP application using [composer](http://getcomposer.org).

The recommended way to install composer packages is:

- For CakePHP 4.0 or greater

```
composer require memorodmx/cakephp-rest-api "^1.2"

```

- For CakePHP 3.0 or greater

```
composer require memorodmx/cakephp-rest-api "^1.1"

```

After installation, [Load the plugin](http://book.cakephp.org/3.0/en/plugins.html#loading-a-plugin)

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

Or, you can load the plugin using the shell command

```
$ bin/cake plugin load -b RestApi
```

Usage
-----

[](#usage)

You just need to create your API related controller and extend it to `ApiController` instead of default `AppController`. You just need to set you results in `apiResponse` variable and your response code in `httpStatusCode` variable. For example,

```
namespace App\Controller;

use RestApi\Controller\ApiController;

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

    /**
     * bar method
     *
     */
    public function bar()
    {
	// your action logic

	// Set the HTTP status code. By default, it is set to 200
	$this->httpStatusCode = 200;

	// Set the response
        $this->apiResponse['you_response'] = 'your response data';
    }
}
```

You can define your logic in your action function as per your need. For above example, you will get following response in `json` format,

```
{"status":"OK","result":{"you_response":"your response data"}}
```

The URL for above example will be `http://yourdomain.com/foo/bar`. You can customize it by setting the routes in `APP/config/routes.php`.

Simple :)

Configurations
--------------

[](#configurations)

This plugin provides several configuration related to Response Format, `CORS` , Request Logging and `JWT` authentication. The default configurations are as below and defined in `RestApi/config/api.php`.

```
