1

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 :'(

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

thank you the answer. but I have many methods in that controller, do i need to make all manual routes? if YES :'( because I have many versions of my API (around 7 versions in one project), do it have any easy way to do that? I mean one configuration for all or for one version.
1

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'
                                       ],
                                      )

                              )
                         );
});

1 Comment

Is it possible to define parameters? I'm trying to get api/{resource}/{id}/validate recognized but no luck so far

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.