Web Services in today's applications and websites have become critical to interacting with third parties. A lot of Drupal developers have the need to expose content and features on their site via an API. Luckily for us, Drupal 8 now has this capability in Core. And some contrib modules are attempting to make it even better.
In this webinar, we will discuss what you can do with Drupal 8, what is included in core, and what is not. Topics covered will include what is new with REST in Drupal 8 and what it aims to solve. Attendees will learn:
- What CRUD is and how to use it
- What you can do with REST in Core
- How to use and build on Services in Drupal 8
- How Drupal 8, Acquia, and Palantir can help you succeed
6. Collection of Resources
Resource identified by URI
Resource represented in a format
Manipulate through Verbs/Methods
Known hyperlinks to other Resources
REST (Hypermedia)
19. Drupal 8 - REST module
→Core module for common pattern of REST usage
→~RestWS
→Uses Serialization
→Define "Resource" Plugins
▪ GET/get(), PUT/put(), DELETE/delete()
▪ Return ResourceResponse
▪ Drupal will serialize()/deserialize() for you
20. Drupal 8 - REST resources
class DBLogResource extends ResourceBase {
public function get($id = NULL) {
if ($id) {
$record = db_query("SELECT * FROM {watchdog} WHERE wid = :wid", [':wid' => $id])
->fetchAssoc();
if (!empty($record)) {
return new ResourceResponse($record);
}
throw new NotFoundHttpException(t('Log entry with ID @id was not found', ['@id' => $id]));
}
throw new HttpException(t('No log entry ID was provided'));
}
}
21. Drupal 8 - REST resources
All Content Entities are supported out-of-the-box
● Uses same URL as HTML page
● All supported formats, automatically
● Opt-in configuration
Easily add your own resources, too
Teach serializer about it (Normalizer)
Create new Resource plugin
Profit!!1!
22. Drupal 8 - Content Negotiation
Reverse proxies suck
+
Browsers suck even more
=
Accept-based negotiation is broken 90% of the time :-(
28. Drupal 8 - Services
→Provide endpoint capabilities to put your API behind a
centralized URL
→Standardize on an approach to building non-REST
APIs
→Accept Header content negotiation
→Gives us regular json response instead of hal_json
→Config entities too!
29. Drupal 8 - ServicesDefinition
→Defines the ‘resource’
→Protein of Services in
D8
→Route Access
→Request method