Developing an REST API in Symfony2 does not differ much (in terms of code organization) from the development of a web system, for example. Some paradigms such as authentication and request / response type, however, are different.
The main libraries I use to develop REST APIs are:
Depending on the needs of your application, I also recommend these libraries:
As I said, what mainly changes is the authentication scheme and how data will be read and written (in JSON or XML, and with the use of HTTP verbs such as GET
, PUT
, POST
and DELETE
).
By FOSRestBundle
documentation, you learn how to configure your drivers to send serialized objects according to the client's request. You can send a single object, a collection of objects, enveloped or not - it's your choice. In addition, by JMSSerializerBundle
, you can fine-tune which attributes of each object will be serialized on each route. For example, in a user list you can only pass the id
and username
of each, but at the time of picking up this user you can return all your attributes.
As for authentication, you obviously will not have a login form and password in order to provide access to that user's protected resources. Usually a basic authorization is used (in which credentials are passed by the Authorization
header of the request using type Basic
).
However, this type of authorization exposes the credentials of the client in all requests, and you can opt for a slightly more structured authentication scheme - using OAuth v2 or another authentication protocol. In this protocol you only pass the credentials on the first call, and use access keys (which expire from time to time) for the following calls. The FOSOAuthServerBundle
library helps you set up an OAuth client, as well as create the tables and provide 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 further questions or need help in implementing any of the libraries I mentioned above, just talk. :)