忍び歩く男 - SLYWALKER

大阪のこっそりPHPer

#CakePHP 爆速でAPIを実装するチュートリアル


f:id:slywalker:20131115100808j:plain
JSJSONXMLAPI

ComposerCakePHP2.4FriendsOfCake/crud使



slywalker/cakephp-app-api_sample

CakePHP


composer.json

composer.json
{
    "require": {
        "pear-cakephp/cakephp": "2.4.*"
    },
    "config": {
        "vendor-dir": "Vendor/"
    },
    "repositories": [
        {
            "type": "pear",
            "url": "http://pear.cakephp.org"
        }
    ]
}

composer.phar
$ curl -s http://getcomposer.org/installer | php
$ php composer.phar install

Bake
$ Vendor/bin/cake bake project $PWD --empty



f:id:slywalker:20131115102539p:plain


CAKE_CORE_INCLUDE_PATH

webroot/index.php, webroot/test.php
define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . APP_DIR . DS . 'Vendor' . DS . 'pear-pear.cakephp.org' . DS . 'CakePHP');

Console/cake.php

Console/cake.php
25c25
<   $root = dirname(dirname(dirname(__FILE__)));
---
>    $app = dirname(dirname(__FILE__));
29c29
<   ini_set('include_path', $root . PATH_SEPARATOR . __CAKE_PATH__ . PATH_SEPARATOR . ini_get('include_path'));
---
>    ini_set('include_path', $app . $ds . 'Vendor' . $ds . 'pear-pear.cakephp.org' . $ds . 'CakePHP' . PATH_SEPARATOR . ini_get('include_path'));
35c35
< unset($paths, $path, $dispatcher, $root, $ds);
---
> unset($paths, $path, $dispatcher, $app, $ds);

database.phpbake
$ Console/cake bake db_config


FriendsOfCake/crudcakephp/debug_kitcomposer.json

composer.json
{
    "require": {
        "pear-cakephp/cakephp": "2.4.*",
        "cakephp/debug_kit": "~2.2",
        "FriendsOfCake/crud": "3.*"
    },
    "config": {
        "vendor-dir": "Vendor/"
    },
    "repositories": [
        {
            "type": "pear",
            "url": "http://pear.cakephp.org"
        }
    ]
}

Composer
$ php composer.phar update

CakePHP

Config/bootstrap.php
CakePlugin::loadAll();

Crud


Crud使

.jsonJSON.xmlXMLroutes.php

Config/routes.php
Router::parseExtensions('json', 'xml');

AppController.php

Controller/AppController.php
<?php
App::uses('Controller', 'Controller');
App::uses('CrudControllerTrait', 'Crud.Lib'); // これ書いといて

class AppController extends Controller {

    use CrudControllerTrait; // トレイト使うよ!なんてモダン

    public $components = [
        'Session',
        'RequestHandler', // これが拡張子で処理をわけてくれるのさ
        'Paginator' => [
            'paramType' => 'querystring' // APIっぽくクエリ形式
        ],
        'DebugKit.Toolbar' => [
            'panels' => ['Crud.Crud'] // Crud用のパネルがあるのさ 
        ],
        'Crud.Crud' => [
            'actions' => ['index'], // とりあえずindexのアクションだけ
            'listeners' => ['Api'] // ApiListenerを使うよ
        ]
    ];

}



MySQL使githubConfig/Schema/cakeapi.sql.gz

1geometry使

Model




Model/Geometry.php
<?php
App::uses('AppModel', 'Model');
App::uses('Sanitize', 'Utility');

class Geometry extends AppModel {

    public $virtualFields = [
        'lat' => 'Y(`latlng`)',
        'lng' => 'X(`latlng`)'
    ];

    public function conditionCenter($queryParams) {
        $queryParams = Sanitize::clean($queryParams) + [
            'lat' => null,
            'lng' => null
        ];

        if (
            !is_numeric($queryParams['lat']) ||
            !is_numeric($queryParams['lng'])
        ) {
            return [];
        }

        return ["MBRContains(
          GeomFromText(
              Concat(
                  'LineString(',
                  {$queryParams['lng']} + 1,
                  ' ',
                  {$queryParams['lat']} + 1,
                  ',',
                  {$queryParams['lng']} - 1,
                  ' ',
                  {$queryParams['lat']} - 1,
                  ')'
              )
          ),
          latlng
      )"];
    }

}

使

Controller




Controller/Geometries.php
<?php
App::uses('AppController', 'Controller');

class GeometriesController extends AppController {

    public function beforeFilter() {
        $this->Crud->on('beforePaginate', function(CakeEvent $event) {
            $model = $event->subject->model;
            $request = $event->subject->request;

            $event->subject->paginator->settings += [
                'conditions' => [
                    $model->conditionCenter($request->query)
                ]
            ];
        });

        parent::beforeFilter();
    }

}






http://localhost/geometries.json
{
    "success": true,
    "data": [
        {
            "Geometry": {
                "id": "1",
                "latlng": null,
                "lat": "84.001196",
                "lng": "191.951974"
            }
        },
        {
            "Geometry": {
                "id": "2",
                "latlng": null,
                "lat": "51.617372",
                "lng": "162.921083"
            }
        },
        ....
    ]
}

20Paginate


http://localhost/geometries.json?limit=1
{
    "success": true,
    "data": [
        {
            "Geometry": {
                "id": "1",
                "latlng": null,
                "lat": "84.001196",
                "lng": "191.951974"
            }
        }
    ]
}



ApiPaginationListener

Controller/AppController.php
<?php
class AppController extends Controller {

    use CrudControllerTrait;

    public $components = [
        'Crud.Crud' => [
            'actions' => ['index'],
            'listeners' => [
                'Api', 
                'ApiPagination'  // これ!
            ]
        ]
    ];

}


http://localhost/geometries.json?page=2&limit=3
{
    "success": true,
    "data": [
        {
            "Geometry": {
                "id": "4",
                "latlng": null,
                "lat": "83.012136",
                "lng": "165.754295"
            }
        },
        {
            "Geometry": {
                "id": "5",
                "latlng": null,
                "lat": "123.59616",
                "lng": "201.408059"
            }
        },
        {
            "Geometry": {
                "id": "6",
                "latlng": null,
                "lat": "80.112906",
                "lng": "177.030496"
            }
        }
    ],
    "pagination": {
        "page_count": 3334,
        "current_page": 2,
        "has_next_page": true,
        "has_prev_page": true,
        "count": 10000,
        "limit": 3
    }
}

(×3)API

CakePHPApiTransformationListener使

Controller/AppController.php
<?php
class AppController extends Controller {

    use CrudControllerTrait;

    public $components = [
        'Crud.Crud' => [
            'actions' => ['index'],
            'listeners' => [
                'Api', 
                'ApiPagination', 
                'ApiTransformation  // これ!
          ]
      ]
  ];

}


http://localhost/geometries.json?page=2&limit=3
{
    "success": true,
    "data": [
        {
            "id": 4,
            "latlng": null,
            "lat": 83.012136,
            "lng": 165.754295
        },
        {
            "id": 5,
            "latlng": null,
            "lat": 123.59616,
            "lng": 201.408059
        },
        {
            "id": 6,
            "latlng": null,
            "lat": 80.112906,
            "lng": 177.030496
        }
    ],
    "pagination": {
        "page_count": 3334,
        "current_page": 2,
        "has_next_page": true,
        "has_prev_page": true,
        "count": 10000,
        "limit": 3
    }
}




http://localhost/geometries.json?lat=35.67832667&lng=139.77044378
{
    "success": true,
    "data": [
        {
            "id": 2583,
            "latlng": null,
            "lat": 35.790109,
            "lng": 140.713021
        },
        {
            "id": 5111,
            "latlng": null,
            "lat": 35.759589,
            "lng": 140.428571
        },
        {
            "id": 6944,
            "latlng": null,
            "lat": 36.627709,
            "lng": 140.225557
        }
    ],
    "pagination": {
        "page_count": 1,
        "current_page": 1,
        "has_next_page": false,
        "has_prev_page": false,
        "count": 3,
        "limit": 20
    }
}



CrudGETPOSTPUTUPDATEDELETE



CakePHPslywalker (Yasuo Harada)