Kannika
Kannika

Reputation: 2558

CakePHP 3 RESTful custom method

In CakePHP 2, I use a custom controller as version of my API something like V01Controller, V02Controller, ... with some method inside and response JSON data.

so when user request a method getUpdate by calling /api/v01/getUpdate.json?lastUpdated=1234567890 or /api/v02/getUpdate.json?lastUpdated=1234567890 it works as expected.

Currently I want to upgrade my CakePHP project to version 3. and I want to keep URL the same as the old CakePHP version 2.

In CakePHP 2, the configuration in routes.php is :

Router::mapResources(array('V01'));
Router::parseExtensions('json');

Here what I try so far but It is not working in CakePHP 3 :

In routes.php:

Router::scope('/api', function ($routes) {
    $routes->extensions(['json', 'html']);
    $routes->resources('V01');
});

In V01Controller:

class V01Controller extends Controller {

    public function initialize() {
        parent::initialize();
        $this->loadComponent('RequestHandler');
    }

    public function index() {
        $result = ['foo' => 'bar'];
        $this->set(compact('result'));
        $this->set('_serialize', ['result']);
    }

    public function getUpdate() {
        $lastUpdated = $this->request->query('lastUpdated');
        // todo
        $result = ['foo' => 'bar'];
        $this->set(compact('result'));
        $this->set('_serialize', ['result']);
    }

}

When the request with this /api/v01.json, it is well response as described in document.

But when I try to request with my old request /api/v01/getUpdate.json?lastUpdated=1234567890 I've got this Error: ApiController could not be found.

Please help me how to custom the method work in CakePHP 3. I don't know if I miss / wrong configuration :'(

Upvotes: 1

Views: 2438

Answers (2)

Rohit
Rohit

Reputation: 538

in routes.php

Router::scope('/api', function ($routes) {
    $routes->extensions(['json', 'html']);
    $routes->resources('V01',array('map'=>
                                  array('getup' =>[
                                       'action' => 'getUpdate',
                                       'method' => 'GET', 
                                       'path' => 'getupdate']
                                      )
                              )
                         );
});

in case if webapp sending OPTIONS before POST

Router::scope('/api', function ($routes) {
    $routes->extensions(['json', 'html']);
     $routes->resources('V01',array('map'=>
                                   array('getup-opt' =>[
                                       'action' => 'getUpdate',
                                       'method' => 'OPTIONS', 
                                       'path' => 'getupdate'
                                       ],
                                       'getup-post' =>[
                                       'action' => 'getUpdate',
                                       'method' => 'POST', 
                                       'path' => 'getupdate'
                                       ],
                                      )

                              )
                         );
});

Upvotes: 1

The action getUpdate is not in the list of index, add, edit, view, delete, so you need to create a route for it or to instruct the resources() method to map your action as described in the manual:

http://book.cakephp.org/3.0/en/development/routing.html#mapping-additional-resource-routes

Upvotes: 1

Related Questions