Developing a REST API in Symfony2 does not differ much (in terms of code organization) from developing a web system, for example. Some paradigms like authentication and request/response type, however, are different.
The main libraries I use to develop REST Apis are:
Depending on your application needs, I also recommend these libraries:
As I said, what changes mainly is the authentication scheme and how the data will be read and written (in JSON or XML, and with the use of HTTP verbs like GET
, PUT
, POST
and DELETE
).
For the documentation of FOSRestBundle
, You learn how to configure your controllers to send serialized objects according to the client’s request. You can send a single object, a collection of objects, enveloped or not - the choice is yours. Also, by JMSSerializerBundle
, you can make a fine-tune in which attributes of each object will be serialized in each route. For example, in a user listing you can only pass the id
and the username
of each, but at the time of picking up this user you can return all its attributes.
As for authentication, you will obviously not have a login and password form in order to provide access to that user’s protected resources. Usually basic authorization is used (in which credentials are passed by the header Authorization
of the request using the type Basic
).
However, this type of authorization exposes the client’s credentials in all requests, and you can opt for a slightly more robust authentication scheme - using Oauth v2 or another authentication protocol. In this protocol you only pass credentials on the first call, and use access keys (which expire from time to time) for subsequent calls. The library FOSOAuthServerBundle
helps set up an Oauth client, in addition to creating the tables and providing an easy way to connect this type of authentication to your application routes.
Anyway, this is the way I work with Apis in REST. If you have any more questions or need help implementing any of the libraries I mentioned above, just say the word. :)