News Archive
PhpRiot Newsletter
Your Email Address:

More information

Me and Symfony2 learning about REST

Note: This article was originally published at Planet PHP on 18 April 2011.
Planet PHP

I hate the frontend. Maybe its just all the scars from the IE4 and IE6 days I had to suffer through, but since then I have taken every opportunity to move myself further away from the frontend. Database-land seemed equally ridden with incompatibilities but for some reason I coped better with that. Anyway, somehow this entire REST thing seemed frontend to me. I think at some point I ended up with the misconception that REST is about URLs. Mea culpa. Thanks to Fabien and his advocacy around learning the HTTP protocol and ESI, I took a second look and realized that there is a lot more to it and that one should care regardless of one considers oneself a frontend or backend guy. Independently I did develop an interest in being able to easily return content in different formats, which let to the creation of the ViewBundle and the MultiplexBundle. but now I am actually trying to learn what REST is all about. And while I am at it, I am teaching Symfony2 as well, et voilA RestBundle!

First up I should say that RestBundle is actually a merge of the ViewBundle and EverzetRestfulControllersBundle developed by Konstantin and Bulat. The goal is to provide a tool chain to quickly develop applications that follow REST principles. I am still far away from being able to lecture on these, but I have tried to put together a list of my recent reading material on the topic. The Bundle provides several optional features. One is the ability to generate routing rules by following naming conventions for Controller actions. Another feature is the ability to parse request headers to determine the proper output format as well as decoding parameters out of the request body. The listener I implemented for this is actually also now integrated in the Silex micro framework. Finally it provides means to handle generation of the final output in a separate view layer. One key result of this separate view layer is the ability to decide how to handle the common use case of having to redirect after a form submission, but not necessarily after creating a new resource via JSON/XML.

I am trying to keep Liip's HelloBundle up to date with examples of how to use all of this. For now there is an example of a controller showing off the route generation and view layer. Another example illustrates how to handle redirection with the view layer, though I still have some todo's here. Finally there is a controller showing off the integration with the FrameworkExtraBundle from Sensio.

The next big steps I want to see happen is adding a normalizer for form instances. The idea being that via a normalizer it will be possible to extract the errors from a form instance and serialize these to JSON or XML to return a response to a resource creation or update from the same Controller that generates an html form otherwise. The other important thing I want to deal with soon is teaching the Serializer component how to lazy load encoders and normalizers (*nudge* Jordi *nudge*). I think the service mapping features of the ContainerWrapperBundle could play a key role there once we get the issues caused by interface injection worked out.

In the mid term I want to see the testing improved as the bits of pieces that I mainly authored are severely lacking in this department. Also the documentation could be organized a lot better and expanded. This would be a prerequisite to be able to get this

Truncated by Planet PHP, read more at the original (another 600 bytes)