For a little while now I’ve been working on a bundle for Symfony to easily create REST apis for Doctrine entities. My goal was to be able to create a REST api with nothing more than a Doctrine entity and it’s metadata, including custom metadata from bundles like JMS Serializer. I wanted to invest very little time in the actual api part, and get to writing applications that used it. I’ve hit this wall before, and inevitably what happens is I make a valiant effort at rolling a REST api and then get distracted or bored and walk away leaving the project to rot.

Recently I was resurrecting an older project and wanting to do right by it and decided I could mask a lot of my database worts by leveraging some of Doctrine’s more advanced features. Specifically I added custom types to ensure I had good data structures, leveraged some lifecycle events and implemented Doctrine 2.5’s recent addition of Embeddables. I had also added a bunch of validation to my entities using Symfony’s Validation component, so they were well defined and could be validated easily with meaningful responses when things weren't right. I wound up with a clean set of objects that did what I wanted well, but there was one problem, I needed an api! Having been down this road before I wanted to solve my problem for the last time, or at the very least make the problem more interesting to solve.

Recently I have been playing with Knp’s JSON Schema bundle and this was in part my inspiration to make something that was magical. I don’t mean magical in the buzz word sense (okay, maybe I do). I mean magical in the highly opinionated, convention driven sort of way. My solution needed to just work(tm). The pieces were all there, and as I mentioned earlier the bulk of the tooling already existed, specifically great serialization, easy validation, reliable content negotiation and of course the graph traversal of Doctrine itself.  I just needed to get it all hooked into the request/response process of an api. Thus begot my very own REST bundle for Symfony 2.

Now hold up, I know what you’re thinking... Symfony already has a REALLY good REST bundle from Friends of Symfony, doesn’t it? This is very true! That is an excellent bundle, it’s high powered and very flexible. Mine is not. In fact it’s not even intended to be. If you want HATEOAS for example, FOSRestBundle is the right option for you. If you need to customize the POST/PUT processes or alter extensively the behavior of your GET actions, then FOSRestBundle is going to be a far better option for you. If you want to use Propel or Mongo (though that may come in the future for mine) or any other data source besides Doctrine ORM than FOSRestBundle is the better option for you. If you are using Doctrine ORM and want to write as little code as possible while getting some REST endpoints, than my bundle might be the better option for you.

Enough chatter, take a look at the fixtures included in my bundle and you’ll see some very simple examples of what sort of metadata driven modeling I had in mind for this project. I have a pretty extensive functional test that shows how the bundle works. I’ve also built a simple example application using this bundle that you can take a look at and see the bundle working. Well without further delay, I'm really happy to share with you LemonRestBundle, documentation included! Feedback is very welcome, feel free to drop me an email or shoot me a message on twitter.


Do you want to get new posts from lemonbytes directly in your inbox? Subscribe today!