PHPackages                             jeffersonsimaogoncalves/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. jeffersonsimaogoncalves/cakephp-rest-api

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

jeffersonsimaogoncalves/cakephp-rest-api
========================================

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

v1.2.0(7y ago)221MITPHPPHP &gt;=7.0

Since Jun 8Pushed 6mo agoCompare

[ Source](https://github.com/jeffersongoncalves/cakephp-rest-api)[ Packagist](https://packagist.org/packages/jeffersonsimaogoncalves/cakephp-rest-api)[ Docs](https://github.com/jeffersonsimaogoncalves/cakephp-rest-api)[ RSS](/packages/jeffersonsimaogoncalves-cakephp-rest-api/feed)WikiDiscussions master Synced 1mo ago

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

RestApi plugin for CakePHP 3
============================

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

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.6.0 or greater.
- PHP 7.0 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:

```
composer require jeffersonsimaogoncalves/cakephp-rest-api

```

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

```
