Example: REST

Same name in other branches
  1. 3.x modules/rest_example/rest_example.module \rest_example

This module will give you a brief demonstration of how the REST service works.

This example uses nodes, because they works out of the box. But any entity could be used.

To complete a demo, we'll need a server site, containing the nodes, and a client, that sends requests to the server. The same site can very well act as both client and server.

First thing you want to do, is visit 'examples/rest-client-settings' and setup what website you want to interact with. To avoid complications, and minimize the chance of failure, I'll suggest that you start using the same site as both server and client, using the user 1 credentials. And when you have gotten the feel of how things work, try to use two separate sites, and maybe another user. Please note that you need to install this module on both client and server site.

Next thing you want is to go to 'examples/rest-client-actions'. This page contains a table where all the remote nodes, of the type rest_example_test, will be displayed. At first there might not be any.

From the same page you can create, edit and delete nodes on the remote site.

The server site actually only uses the View (rest_service) and the node type (rest_example_test), imported upon installation of this module. The rest of the functionality comes from Drupal Core modules, like 'rest', 'basic_auth' and 'hal'.

The client part of the site uses the following classes:

See also

\Drupal\rest_example\Controller\RestExampleClientController::indexAction() Builds the list of nodes on the remote server.

Parent topics

File

modules/rest_example/rest_example.module, line 8

Classes

Title Sort descending File name Summary
RestExampleActionTest modules/rest_example/tests/src/Functional/RestExampleActionTest.php Verify that the Views are accessible.
RestExampleClientController modules/rest_example/src/Controller/RestExampleClientController.php Controller class for the REST Example routes.
RestExampleClientDelete modules/rest_example/src/Form/RestExampleClientDelete.php Delete a new node on a remote Drupal site.
RestExampleClientEdit modules/rest_example/src/Form/RestExampleClientEdit.php Edit or create a new node on a remote Drupal site.
RestExampleClientSettings modules/rest_example/src/Form/RestExampleClientSettings.php Setup form for a Drupal REST client.
RestExampleHelpController modules/rest_example/src/Controller/RestExampleHelpController.php Provides a help page for the REST Examples module.
RestExampleMenuTest modules/rest_example/tests/src/Functional/RestExampleMenuTest.php Test the user-facing menus in Rest Example.
RestExampleSettingsTest modules/rest_example/tests/src/Functional/RestExampleSettingsTest.php Test the settings in Rest Example.