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