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

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

pejonic/cakephp-3-rest-api
==========================

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

v1.1.3(5y ago)16MITPHPPHP &gt;=5.5.9CI failing

Since May 1Pushed 5y agoCompare

[ Source](https://github.com/johnnwanosike1/cakephp-3-Rest-API)[ Packagist](https://packagist.org/packages/pejonic/cakephp-3-rest-api)[ Docs](https://github.com/cakephp/debug_kit)[ RSS](/packages/pejonic-cakephp-3-rest-api/feed)WikiDiscussions master Synced 5d ago

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

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

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

[![Build Status](https://camo.githubusercontent.com/326aa84a663d5ae143b9bd5ac1d5ff96430a7f121c595b351aefa41c0ffafac2/68747470733a2f2f7472617669732d63692e6f72672f6d756c7469646f74732f63616b657068702d726573742d6170692e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/multidots/cakephp-rest-api)

This plugin provides basic support for building REST API services in your CakePHP 3 application.

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

[](#requirements)

This plugin has the following requirements:

- CakePHP 3.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:

```
composer require pejonic/cakephp-3-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)

### cors

[](#cors)

As of now, this plugin provides configuration for CORS requests. By default, cors requests are enabled and allowed from all domains. You can overwrite these settings by creating config file at `APP/config/api.php` . The content of file will look like,

```
