There has been a growing trend to start offering RESTful services for any web product, and Joomla is no exception. With Joomla, however, there is no "preferred" way of writing RESTful services so everyone (that includes us) is trying to build their own component and plugin to enable RESTful services in Joomla. We did the very first REST implementation with Joomla 1.5 which was made as a JSON based alternative to the then existing XML-RPC system that was already present. This blog attempts to list out the various approaches available and the benefits of using each.
Using the JSON document type (format=json)
This is perhaps the oldest and most common way to do JSON over REST. This method is supported inherently by Joomla and developers just need to provide a view.json.php into each of their views to expose the data from the model via JSON. The framework on Framework (FOF) goes a step further and you don't need to create this file if the default output from the model is sufficient to you.
- No extra extensions needed
- Present directly inside a component so very easy to use model methods
- No good way of doing POST requests, so ideal only for fetching data
API component (com_api)
com_api is a component maintained by Techjoomla which follows a plugin based architecture. Developers write apps (plugins) that contain multiple resources. Eg: The user app may have a resource for fetching and saving user data and another resource for login. Similarly, the content app may have resources for articles & categories. This allows implementing all the GET, POST, PUT & DELETE methods.
- Very close to a true REST implementation
- Plugin structure allows flexibility in implementing REST resources
- Complex data models can be created since none of the resources is bound directly to any model
- Possible to introduce custom error handling
- Likely to introduce redundant code if model methods cannot be reused for fetching and saving of data
Expanding on the POC web services app
Understanding the need for web services, a very detailed API specification document has been created by Chris Davenport for the web services working group. The POC contains a few sample resources and a suggested/possible implementation of the web services. While this specification does not actually specify how the implementation should be done, it's more of a guideline for the URLs and data structures. A working example of the POC is here.
- Once it's firmed up it'll be the default way to do APIs in Joomla
- Multilingual support
- In early stages of development
Is there a better way we've missed out? Have anything to add/improve on these? Let us know in the comments below!