![]() Once REST resources are developed, they still require configuration. Line 124 demonstrates the response triggered by returning a ResourceResponse object with the desired data. In this example, I’ve set it to not cache requests. Lines 117-121 demonstrate specific caching directives that can be used at your discretion. Responses can be cached from the endpoint to help with performance. All code within this method is used to generate the response. Line 99 demonstrates a get() method that corresponds to a GET request available from the endpoint. Drupal’s REST resource plugins support requests by defining specific methods within the class. Web service endpoints define the expected methods of the request. This demonstrates how to extend your REST resource for any additional class attributes. The request and current user extend the constructor of the ResourceBase class and saved into the currentUser and request attributes of a class instance defined in lines 35 and 42 respectively. In this example, I load serialization formats, a specific logger, the current user, and the current request from Drupal’s runtime container. The use of an overridden _construct and the complementary create functions demonstrate how we use dependency injection to load additional system resources into our class. Extending ResourceBase as a base class allows us to inherit standard REST resource functionality and only needing to develop our specific functionality within our class. Line 28 demonstrates the name of the custom class, which should match the filename per object oriented best practice. Base ClassĬlasses are capable of implementing interfaces (a common, shared definition of expected functions) or extending base classes that have defined functions. Lines 23 and 24 define the endpoint for the API, for which requests can be made. ![]() Most notably, a unique ID (no spaces and use of underscores) is shown in line 20 and a human recognizable label on line 21. Lines 16-27 demonstrate the annotation for the REST resource class. AnnotationĪnnotations define static attributes and configuration of the REST resource. This is a common pattern leveraged by Drupal’s Plugin API. If the PHP class is placed into a module’s src/Plugin/rest/resource directory, Drupal will automatically register the class and knows, given the path, to associate it with a REST resource. Let’s walk through the code to get an understanding. This example demonstrates an exposed GET endpoint found at the path /api/custom/arnold. Įxample 1: modules/my_custom_module/src/Plugin/rest/resource/ M圜ustomResource.php The REST resource will automatically get discovered after clearing the cache. Create any missing directories as needed. Place a new M圜ustomResource.php class in your module’s src/Plugin/rest/resource directory. DevelopmentĪssuming you have a custom module already created and enabled, we can leverage the native autoloading features to register a new plugin class for a custom REST resource. The following article describes how to create a custom REST endpoint in Drupal 8, as we share how to conditionally return a quote from a provided Arnold Schwarzenegger movie. But, Drupal’s framework has support for any customized needs. Since Drupal is so robust out of the box, it often minimizes the need for custom development. This includes roles/permissions, Rest Web Services, Serialization, Views, and more. Core offers many complementary out-of-the-box features to publish web services and configure them in different ways. This offers Drupal a competitive advantage when creating a decoupled application or building Drupal as part of a larger enterprise of systems. Drupal 8 is a great platform for storing structured data and exposing web service endpoints.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |