Skip to main content

Blog Post

Drupal 8: RESTful Services

by Peter Mallett
September 17, 2013

Our next webinar in the six part series on Drupal 8 is given by Peter Mallett.

In this webinar Peter summarizes the meaning of Services in Drupal 8, why the initiative is happening, a few features with a demo, and some of the rules of REST. The demo is running on a pre-alpha 3 development snapshot of Drupal 8.



Peter elaborates on a question heard from the presentation that asked if it would be possible to create custom services endpoints that still utilized "verbs" in the resource URI. His answer stated that you should encapsulate the logic elsewhere in the application, so that the service resources would only need to be used with POST, PATCH, etc. This example will better illustrate what he was trying to convey:

Imagine we have resources: teacher, class, student, assignment, and task.

One thing we might want to do is assign a task to every student in a teacher's class based on an assignment entity. One way to do this, and sadly I admit I have done this, would be to simply create a resource for /create-assignment, and pass it the assignment object. Next, the service would need to look up the teacher, class, and student. Then, the service would create a task for each student. This is not Restful at all.

A much better way to do this would be:
1) A teacher user clicks "assign task to students" on the assignment screen
2) The application makes a GET request to the resource for the assignment's class and receives the class resource, which contains a list of the students in that class
3) The application could then ask for confirmation to "assign task to X number of students?"
4) The user confirms the assignment
5) The application constructs a task entity for each student and makes a POST to the task URI to create the new task resources

In the end we have the same result. However, now the service side of the scenario remains restful. The logic of creating and assigning the tasks to the students all resides in the application instead of being embedded in the service.

Meet team member, Peter Mallett

Peter has a passion for computer programming that spans over 20 years. He brings over ten years of dedicated Drupal experience to his role as a Senior Architect at Mediacurrent.

Peter has been involved with computers and programming since high school, and has a knowledge base in web architecture. He has over 20 years of PHP development experience, starting with version 3.

Prior to joining Mediacurrent, Peter was part of an award-winning software engineering team with a large interactive agency. During this time, he was involved at all levels of Drupal development, and is well-versed in writing custom modules, theming, and quality assurance. In addition to his years of hands-on Drupal experience, Peter has a well-rounded skill set that includes MySQL, JavaScript, jQuery, HTML, CSS, C, Python, and Photoshop.

Learn more about Peter >

Related Insights