berrygoudswaard/cakephp-api

Installs: 1 801

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 0

Open Issues: 0

Type:cakephp-plugin

4.2.0 2021-10-13 14:24 UTC

This package is not auto-updated.

Last update: 2024-11-07 05:02:08 UTC


README

This plugin contains a component that make it easier to create API output with CakePHP

Requirements

  • CakePHP 3.1+

Installation

composer require berrygoudswaard/cakephp-api

Usage

In your app's config/bootstrap.php add:

Plugin::load('BerryGoudswaard/Api');

Then load the component where you need it by adding the following code to your controller:

$this->loadComponent('BerryGoudswaard/Api.Api', [
    'cors' => [
        'allowHeaders' => ['Content-Type', 'Authorization'],
        'allowMethods' => ['GET', 'POST', 'PUT', 'DELETE'],
        'allowOrigins' => ['http://localhost:4200', 'http://www.berrygoudswaard.nl'],
        'allowCredentials' => 'true',
    ]
]);

Output data

public function index()
{
    $tags = $this->Tags->find();

    $this->Api->addData('tags', $tags);
    return $this->Api->output();
}

The code above will output something like:

{
    "message": "OK",
    "code": 200,
    "data": {
        "tags": {
            "items": [
                {
                    "id": 1,
                    "tag": "cakephp"
                }, {
                    "id": 2,
                    "tag": "plugin"
                }
            ]
        }
    }
}