2017 © Pedro Peláez
 

cakephp-plugin api

API components for CakePHP

image

hiromi2424/api

API components for CakePHP

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 12 Versions
  • 1 % Grown

The README.md

APIコンポーネント

Build Status Downloads Latest Version License, (*1)

要件

CakePHP 2.5+
PHP 5.4.16+

概要

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

セットアップ

  • Configure::write('Routing.prefixes', ['api']);app/Config/core.php で設定
  • app/Config/routes.php で以下のルートを作成(プラグインのルートがプレフィックスよりも優先されるため。 require CAKE . 'Config' . DS . 'routes.php';の前に設置)
Router::connect('/api/:controller/:action/*', ['api' => true]);
Router::connect('/api/:controller/*', ['action' => 'index', 'api' => true]);
  • CakePlugin::load(Api); OR CakePlugin::loadAll()app/Config/bootstarp.php で設定
  • 使いたいコントローラ(OR AppController)で以下のように設定
$components = [
    'Api.Api'
];
  ```

### コーディング例

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

```php
/**
 * signup api
 *
 * @return void
 */
public function api_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 api_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 を見ると言い換えることもできます。, (*3)

レスポンス概要

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

レスポンス例(成功), (*4)

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

レスポンス例(失敗), (*5)

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

注意事項

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

The Versions

20/09 2016

3.0.x-dev

3.0.9999999.9999999-dev https://github.com/hiromi2424/api

API components for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

api cakephp

20/09 2016

v3.0.7

3.0.7.0 https://github.com/hiromi2424/api

API components for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

api cakephp

18/08 2016

v3.0.6

3.0.6.0 https://github.com/hiromi2424/api

API components for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

api cakephp

18/08 2016

dev-master

9999999-dev

API components

  Sources   Download

MIT

The Requires

 

18/08 2016

v3.0.5

3.0.5.0 https://github.com/hiromi2424/api

API components for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

api cakephp

23/05 2016

v3.0.4

3.0.4.0 https://github.com/hiromi2424/api

API components for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

api cakephp

23/05 2016

v3.0.1

3.0.1.0 https://github.com/hiromi2424/api

API components for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

api cakephp

18/05 2016

v3.0.0

3.0.0.0 https://github.com/hiromi2424/api

API components for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

api cakephp

27/02 2015

v1.0.3

1.0.3.0

API components

  Sources   Download

MIT

The Requires

 

26/02 2015

v1.0.2

1.0.2.0

API components

  Sources   Download

MIT

The Requires

 

18/02 2015

v1.0.1

1.0.1.0

API components

  Sources   Download

MIT

The Requires

 

13/02 2015

v1.0.0

1.0.0.0

API components

  Sources   Download

MIT

The Requires