Quickly Build RESTful APIs in PHP with Slim

Writing and interacting with APIs, RESTful APIs in particular, is something most web developers have to deal with. There are a number of ways to implement them, and many of them are quite straightforward, like on the Java side of the things with JAX-RS. With PHP, one of the best options is the Slim Framework.

Slim is actually a PHP micro framework that can be used for far more than just writing RESTful APIs. Digging into the documentation reveals that there is some very powerful routing functionality in there, especially with the slick middleware functionality.

Creating the basis of your RESTful services with Slim is almost embarrassingly easy:

With that small bit of code, Get, Post, Put, and Delete are now all functional and ready to be utilized. Obviously they need to be wired up to do something useful, but still, it’s refreshing to have something so simple provide so much functionality. If you wanted to separate out the functions from the route setup, and not use anonymous functions, that’s also very easy: