#1
After quickly spinning up a backend using Trongate I realised that an API with a single endpoint for handling requests would be more suited to the use case required for the frontend, and would align with best practices for REST API's by not including the verb in the endpoint.

For example: /api/v1/module

All incoming GET, POST, PUT, DELETE requests could go to the same endpoint, whilst having the other endpoints available as well.

A quick hack solution was to create a "v1" function in the engine/api.php file that would route the incoming request to the relevant endpoint. (I did a nested if/else but a switch statement would also be fine.)

A few other tweaks had to be done to the other methods to handle this but seems to work fine.

Is this something that could be considered going forward? What are the pros and cons of doing it this way?

Thanks in advance
G
#2
Just quoting something I hear, or heard. Always looking for Contributors.
#3
Thanks for the reply. Looking forward to the update.

This is what I tried:

function v1() {
$module_name = segment(3);
$this->_make_sure_table_exists($module_name);
$module_endpoints = $this->_fetch_endpoints($module_name);
$endpoint_name = "V1";
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$this->_process_get_with_get($module_name, $module_endpoints, $endpoint_name);
} else if ($_SERVER['REQUEST_METHOD'] == 'DELETE'){
$this->delete();
} else if ($_SERVER['REQUEST_METHOD'] == 'PUT'){
$this->update();
} else {
$this->_process_get_with_post($module_name, $module_endpoints, $endpoint_name);
}
}

A few minor tweaks on the other methods to cover the cases and appears to be working. I am no doubt missing something though.