Why would a sane programmer use PHP rather than Clojure for a RESTful API?

(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com, or follow me on Twitter.

This question on StackOverflow seems a bit sad:

I did look at both Laravel, Sympfony2 and Codeigniter for this REST Api. They all had some elements I liked and some I disliked. My main concern was how to do the authentication because I had a rather complex algorithm where my users can log in with the apps’ access_token or access_tokens served by Google or Facebook. I also perfer being in complete control of my framework and the frameworks mentioned above had some elements I felt unnecessary and hard to work around. Because of this I decided to make my own REST-solution. It is not as hard as one might expect, and it can be done in several ways. The way I did it requires some knowledge about OOP-programming.

Okey, so starting out a made a base-class called REST. This class takes care of all the stuff that is in common for every call. Like authentication, parsing the requested path to a method, checking access_token etc.

One of the central things in this class is the requested path and how this is translated into a method. I did this inspired by Laravel. I have a array with key => value where the key is the url it should match and the value is the actual method to call. I also included the way Lavavel parses variables in the URL like so:

‘/user/(:id)’ => ‘user_id’,
This would match any /user/[number]. It also checks what type of request this is, so if this is a simple get-method it would try to call get_user_id. Anything parsed with (:id) would be used as an argument when calling that method (so it is actually calling get_user_id($id)).

After authentication the actual method-call gets evaluated. I did not want all the methods (like get_user_id) in the REST-class itself, so I broke these up in different controllers that extends the REST-class. This is done by looking at the url being requested. If it is /user/(:id) the script will check if there is a controller named userController.php. If it exists, check if the method we are going to call exists. If it does, check if the number of arguments matches what we have. If everything is good, execute the method, if not return an error message. Structure and error-messages are very important when making a API like this.

In a single day I built out a great RESTful API, using Liberator.

Post external references

  1. 1
    http://stackoverflow.com/questions/15327745/good-php-rest-api-library
  2. 2
    http://clojure-liberator.github.io/liberator/
Source