I am developing a cross-platform system and I need to make a rest API to tie them together. I have long experience in PHP and I want to use this for this service.
I could develop a API 100% manually, but I am hoping there is some great libraries out there that could ease my development.
Does anyone have any experience with libraries like this? Anything you could recommend?
I got the badge
Popular question
for this question, so I feel it's about time I elaborate how I did my REST-soluton.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: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 callingget_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 nameduserController.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 the different controllers I call the constructor for the REST-class to get the authentication, parsing of the url etc resolved. The tricky part here is that I did not want to do:
In the bottom of every controller. So I made a little hack and a script called
run.php
that does this dynamically for every controller-class. Before I include therun.php
I save the path for the controller by simply doing$path = explode('/',__FILE__);
. This is used in the run-script. The run-script looks like this:I found this to be a perfect solution for how I wanted to build my API. I can easily add new methods by supplying it in the array that parses urls to methods, and I can add new methods in the nicely broken apart controllers for maximum cleanness.
Some people might think this is too much work, but it actually took me only a few hours to get it up and running. I'd also call this highly dynamic as I can just add new controllers and the system will recognize them if they are valid url-patterns.
A few friendly advices.
If you decide to go with something resembling this solution, these can be some good tips. In each controller, do something like this:
We will need to store what class we are currently working from. This would be
UserController
or something like that.In the REST-class I can then use this variable to check if the actual method getting called does exist in this controller. I've done that this way:
I hope that can get you all going.
Happy codin'
I had the same question three months ago. I spent many hours researching the best PHP frameworks to use. In the end I settled on Laravel.
Right out of the box it comes with RESTful routes and controllers, and easy to use authentication. I had a simple REST API up and running in about a day
http://laravel.com/docs/routing#the-basics
http://laravel.com/docs/controllers#restful-controllers
It also comes with a great ORM which makes setting up resources super easy
http://laravel.com/docs/database/eloquent
I am using version 3.2 and it works like a charm and is stable. Version 4 is still in beta, but does have a lot more REST orientated features (i think one is that it makes it easier to create resources from within your controllers)
Great tutorial here http://net.tutsplus.com/tutorials/php/laravel-4-a-start-at-a-restful-api/