You are here

Example: REST in Examples for Developers 3.x

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 sugest 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 works, try to use two seperate 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_serice) and the node type (rest_example_test), imported upon installatin of this module. The rest of the functionallity comes from Drupal Core modules, like 'rest', 'basic_auth' and 'hal'.

The client part of the site uses the following classes:

  • Builds the list of nodes on the remote server.
  • A form that can creates/edits nodes.
  • A form used to confirm the deletion of the node.
  • A form wher you setup what site you want to interact with.
  • This class handles all the calls to the remote site, and returns the response back, to be processed by the requesting method.

See also

\Drupal\rest_example\Controller\RestExampleClientController::indexAction()

\Drupal\rest_example\Form\RestExampleClientSettings

\Drupal\rest_example\RestExampleClientCalls

Parent topics

File

modules/rest_example/rest_example.module, line 8
This is a demonstration of how to the REST service in Drupal works.

Classes

Namesort descending Location Description
RestExampleActionTest modules/rest_example/tests/src/Functional/RestExampleActionTest.php Verify that the Views are accessible.
RestExampleClientController modules/rest_example/src/Controller/RestExampleClientController.php Controller routines for 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.