I have read many about REST api in php articles. but I still get quite confusing.
they basically rewrite the url to a index.php, which process the url and depends on the method, then send response
but which is the properly way to process the url? this looks doen’t look correct…
- get the uri and split it
- I should know what to do with each portion, eg. for GET /usr/1 I should do something like:
if($myUri[0]==”usr”)
getUser($myUri[1]);
if the request url is like GET www.domain.com/user/1
it would call getUser($id);
but what happen if you can also retrieve the user by name, or maybe e-mail? so the url can also be www.domain.com/user/john or www.domain.com/user/john@gmail.com
and each url should call different methods like getUsrByName($name) or getUsrByEmail($mail)
The proper way of handling this would be to have URLs like this:
However, specifying multiple “parameters” is more like a search, I’d go against using resources for that, because a path should be absolute. Which means:
And:
Are not the same resource. Instead I’d do:
This is what Stack Overflow uses: