PHPackages                             hiromi2424/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. [API Development](/categories/api)
4. /
5. hiromi2424/api

ActiveCakephp-plugin[API Development](/categories/api)

hiromi2424/api
==============

API components for CakePHP

v3.0.7(9y ago)05971MITPHP

Since Feb 13Pushed 9y ago1 watchersCompare

[ Source](https://github.com/hiromi2424/api)[ Packagist](https://packagist.org/packages/hiromi2424/api)[ Docs](https://github.com/hiromi2424/api)[ RSS](/packages/hiromi2424-api/feed)WikiDiscussions 3.0 Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (12)Used By (0)

APIコンポーネント
==========

[](#apiコンポーネント)

[![Build Status](https://camo.githubusercontent.com/50f0070c46d32e1b0f1c4fe9582093d233a044b57127ac52047676044d4af109/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f6869726f6d69323432342f6170692e706e673f6272616e63683d332e30)](http://travis-ci.org/hiromi2424/api)[![Coverage Status](https://camo.githubusercontent.com/47b48119710141e58edb7f1028a1055b68c039211d724ccbfa57a15241fb4a8f/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6869726f6d69323432342f6170692e7376673f7374796c653d666c61742d737175617265)](https://codecov.io/github/hiromi2424/api)[![Downloads](https://camo.githubusercontent.com/da1de56316f4ed600bffc6c9dc18b90095fd4696361a8494345046ef20d79d6d/68747470733a2f2f706f7365722e707567782e6f72672f6869726f6d69323432342f6170692f642f746f74616c2e706e67)](https://packagist.org/packages/hiromi2424/api)[![Latest Version](https://camo.githubusercontent.com/1b43c9f7ca0035b05793b327df5c3bb6ccb6e7057bae4761a8573233e68bb605/68747470733a2f2f706f7365722e707567782e6f72672f6869726f6d69323432342f6170692f762f737461626c652e706e67)](https://packagist.org/packages/hiromi2424/api)[![License](https://camo.githubusercontent.com/a305b4129ae7c6af5f5002b18c14eeee3ade5300acb61d49eb0f1fd574271b21/68747470733a2f2f706f7365722e707567782e6f72672f6869726f6d69323432342f6170692f6c6963656e73652e737667)](https://packagist.org/packages/hiromi2424/api)

⚠️ 3.0ブランチはドキュメント整備中です。実装とテストは更新済ですが、いくつかの2系で使えていた機能が削除されています。

要件
--

[](#要件)

```
CakePHP 3.1*
PHP 5.5+

```

概要
--

[](#概要)

CakePHPでREST APIを実現するためのコンポーネントと、その付属ライブラリ群です。 基本的にはコンポーネントを設置し、`Routing.prefixes`に`'api'`を含めるだけで使えます。

### セットアップ

[](#セットアップ)

- `app/Config/routes.php` で以下のルートを作成する

```
Router::prefix('api', function ($routes) {
    // ルート定義
});
```

- `Plugin::load('Api');` OR `Plugin::loadAll()` を `app/config/bootstarp.php` で設定
- 使いたいコントローラ(OR `AppController`)で以下のように設定

```
$components = [
	'Api.Api'
];
// または
$this->loadComponent('Api.Api');
```

### コーディング例

[](#コーディング例)

#### 新規登録（メールアドレス）

[](#新規登録メールアドレス)

```
/**
 * signup api
 *
 * @return void
 */
public function signup()
{
	$this->request->onlyAllow('post');
	$this->Api->recordMap = [
		'User' => [
			'name',
			'email',
			'password',
		],
	];
	$params = $this->Api->requireParamsFromMap();
	$data = $this->Api->paramsToRecord($params);

	$this->Api->processSaveRecord($data, [
		'saveCallback' => [$this->User, 'signup'],
	]);
}
```

#### ログイン

[](#ログイン)

```
/**
 * login api
 *
 * @return void
 */
public function login()
{
	$this->request->onlyAllow('post');

	$data = $this->Api->requireParams([
		'email',
		'password',
	]);
	$this->request->data = ['User' => $data];
	$this->Auth->logout();
	$this->User->useValidationSet(['Login']);
	$this->User->create($this->request->data);
	$loggedIn = $this->User->validates() && $this->Auth->login();
	if ($loggedIn) {
		$this->Api->success();
	} else {
		$this->Api->raiseValidationErrors();
	}
}
```

#### コメント取得

[](#コメント取得)

```
App::uses('LackParametersException', 'Api.Error');

Class CommentsController extends AppController {

	/**
	 * index api
	 *
	 * @return void
	 */
	public function api_index($postId = null) {
		$this->request->onlyAllow('get');
		if ($postId === null) {
			throw new LackParametersException('postId');
		}

		$this->Api->recordMap = [
			'Comment' => [
				'id',
				'body',
				'created',
				'updated',
			],
			'User' => [
				'_wrap' => 'user',
				'id',
				'name',
			],
		];

		$this->Comment->create();
		$this->Comment->set('post_id', $postId);
		if (!$this->Comment->validates(['fieldList' => ['post_id']])) {
			$this->Api->recordMap = ['Comment' => ['post_id']];
			return $this->Api->processValidationErrors();
		}

		$limit = 20;
		$page = $this->Api->collectParam('page');
		$page = $page ? (int)$page : 1;
		$contain = ['User'];
		$comments = $this->Comment->findAllByPostId($postId, compact('limit', 'page', 'contain'));

		foreach ($comments as &$comment) {
			$comment = $this->Api->recordToParams($comment);
		}
		$this->Api->success(compact([
			'page',
			'limit',
			'comments',
		]));
	}

}
```

### リクエストの取り扱い

[](#リクエストの取り扱い)

リクエストパラメータとして、GETの場合クエリストリングを、その他の場合はPOST BODYを取り扱います。 `$this->request->query` か `$this->request->data` を見ると言い換えることもできます。

### レスポンス概要

[](#レスポンス概要)

- JSONのみサポートします。
- レスポンス構造は固定です。

レスポンス例（成功）

```
{
    "success": true,
    "code": 200,
    "data": {
        "user": {
            "id": "2081",
            "last_name": "shimizu",
            "first_name": "hiroki"
        },
    }
}
```

レスポンス例（失敗）

```
{
    "success": false,
    "code": 400,
    "errorCode": "validation_error",
    "errorMessage": "バリデーションエラー",
    "validationErrors": {
        "id": [
            [
                "exists",
                "valid"
            ]
        ]
    }
}
```

### 注意事項

[](#注意事項)

- 汎用的な作りにはなっていません。
- 既存のアプリケーションに適用するのは困難です
- ドキュメント化されていない不完全な機能が一部あります(habtm、hasMany対応など）
    - 通常使用には問題ありません

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~58 days

Recently: every ~30 days

Total

11

Last Release

3527d ago

Major Versions

v1.0.3 → v3.0.02016-05-18

### Community

Maintainers

![](https://www.gravatar.com/avatar/4fe1202888a1e909332ee0eda4eb3c017e5ae58912e9777146c97bb61f126ee8?d=identicon)[hiromi2424](/maintainers/hiromi2424)

---

Top Contributors

[![hiromi2424](https://avatars.githubusercontent.com/u/191297?v=4)](https://github.com/hiromi2424 "hiromi2424 (29 commits)")

---

Tags

apicakephp

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/hiromi2424-api/health.svg)

```
[![Health](https://phpackages.com/badges/hiromi2424-api/health.svg)](https://phpackages.com/packages/hiromi2424-api)
```

###  Alternatives

[friendsofcake/crud-json-api

Listener for building CakePHP Crud APIs following the JSON API specification.

58445.4k3](/packages/friendsofcake-crud-json-api)[bcrowe/cakephp-api-pagination

CakePHP 4 plugin that injects pagination information into API responses.

3953.5k1](/packages/bcrowe-cakephp-api-pagination)[mixerapi/mixerapi

Streamline development of API-first applications in CakePHP

4441.8k](/packages/mixerapi-mixerapi)[a2design-company/mandrill-cakephp-plugin

Mandrill CakePHP plugin

193.2k](/packages/a2design-company-mandrill-cakephp-plugin)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
