#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
Hello,

Thanks for that. Having spent quite a lot of time chatting to ChatGPT over this it would appear that the web development community has not yet standardised Restful API practices. That's my best attempt at saying "anything goes".

In the case of PHP, 'PUT' and 'DELETE' requests are treated the same as 'POST' requests. In other words, from PHP's perspective, 'POST', 'PUT' and 'DELETE' requests are all treated like 'POST' requests. So, whilst your idea might work for NodeJS I'm not sure if it would work for PHP.

Having said all that, I've completely rebuilt the API Explorer and have a variety of improvements coming up which are all designed to put us more in line with the rest of the PHP community.
#4
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.