Headless Drupal

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
venkatadapa's picture

Drupal is one of the world’s popular and widely used CMS frameworks. Drupal back-end system is very strong and can be used for managing any type of content like text, categories, media, geospatial etc.

The term Headless Drupal means, use Drupal as backend system and Rest server, so that content operations (create, read, update, delete) can be done from other language or tool. Drupal 8 comes with Rest Server included in its core. In Drupal 7 we can achieve this with a contributed module called Services or RestWS.

Nowadays people are using many devices like desktops, laptops, mobiles, tablets to explore the content and in parallel front-end frameworks are also invented to provide better user experience while viewing content among different devices.

Since most of the people are using smart phones these days, many businesses wants to engage people by providing their services through mobile apps and attract people with some offers and ads.

About REST (Representational State Transfer) API
Restful API gained more popularity than SOAP or WSDL based web services because of faster in development and easier in usage. Restful web services will be resource based. Even though there is no strict rules defined for RESTful API creation, there are some principles defined to design restful web services that focus on application resources like how resource states can be changed and transferred over HTTP. Resource can be anything like user, article, category etc…
There are four basic HTTP methods available to Create, Read, Update and Delete resources on server.

GET : To retrieve resource from server
POST : To create resource in server
PUT : To update a resource on server
DELETE : To delete a resource from server
A good developer should always follow these principles correctly to maintain consistency. See the below example

Bad Practice : GET /api/addArticle?name=Foo HTTP/1.1
It's not a good design because the above web method addArticle supports a state-changing operation over HTTP GET and upon successful execution new article with name Foo will be added to database. Web servers are designed to respond to HTTP GET requests by retrieving resources that match the path (or the query criteria) in the request URI and return these in a response, not to add a record to a database. GET method will be used when retrieving articles like GET /api/getArticles HTTP/1.1

Good Practice : POST /api/addArticle?name=Foo HTTP/1.1
Above one is the good practice to add new resource (article) to the database, so client will set correct method (POST) respectively while sending request.
Another better practice will be something like below, we don’t even need add/delete in the url, it is well understood because of the request type.
POST /api/article
GET /api/article/1
PUT /api/article/1
DELETE /api/article/1

The response from server can be either XML or JSON.
Most people prefer JSON as the response type since it is lightweight and can be parsed easily by any client side language. Drupal Services module can expose the resources nodes, users, taxonomies etc. in Restful way. With custom code too we can deliver a JSON response instead of HTML by setting 'delivery callback' as 'drupal_json_output' in hook_menu implementation.

How Front End Frameworks Interact with REST Server
All front end (MVC JavaScript) frameworks will have capabilities to process http requests, and Angular JS is one among them which comes bundled with many services. $http is one of the services to process HTTP requests and on top of it there is another service called $resource that lets you interact with Restful servers (back-end data stores) easily.

Using Headless Drupal, content rich back-end system can be done very quickly and data can be exposed through REST API. Front-end can be done by using any of the desired front end framework like Angular JS, Backbone or any other similar framework. We can also use the same REST API for developing mobile applications.
This way we can build different kinds of applications like single page application (SPA), responsive and mobile apps with single Drupal back-end system.

Comments

I've used Drupal as a backend

Hadi Farnoud's picture

I've used Drupal as a backend of an iPhone app before. It was a pain to do. Now that I think about it, it was probably easier to just write the whole thing from scratch.

my question is, would headless drupal ease that pain? now that frameworks like Laravel and Ruby on Rails exist, is it worth using Drupal?

How would drupal react for simple data fetching API???

irfworld's picture

Suppose I have a custom API which provides simple node data in json by drupal_json_output and using drupal mysql API.

So will Drupal do the full bootstrap (Config., Cache, DB, Variables, Session, etc..... ) for this API hit ?

If yes, Then what would be the best solution for this?

Yes, Most the standard Drupal

Greg Boggs's picture

Yes, Most the standard Drupal stuff is required to start an API. But, a huge chunk of Drupal's overhead is in the front end render which won't execute. I've delivered Drupal APIs based on services module to tens of thousands of users on a single web server with no troubles, extremely fast performance and very low load.

Headless Drupal

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week