Example: Session

Same name in other branches
  1. 4.0.x modules/session_example/session_example.module \session_example

Demonstrating how to manipulate sessions in Drupal 8.

You can read the API documentation here: https://api.drupal.org/api/drupal/core%21core.api.php/group/session/

Drupal's session management is inherited from Symfony. We use the Symfony\Component\HttpFoundation\Session\SessionInterface object which we can get from the Request object.

This object allows us to get and set values from the user session.

We can use the session to store information per visitor.

In this example, we will have the user fill out a form with some personal information, and then be able to display that information on another page.

To learn more, one could examine the Stream Wrapper Example. It uses the session API to implement a session: file scheme.

See also

Example: Stream Wrappers

https://api.drupal.org/api/drupal/core%21core.api.php/group/session/

Parent topics

File

modules/session_example/session_example.module, line 8

Classes

Title Sort descending File name Summary
SessionExampleController modules/session_example/src/Controller/SessionExampleController.php Controller for a page to display the session information.
SessionExampleForm modules/session_example/src/Form/SessionExampleForm.php Form to allow the user to store information in their session.
SessionExampleTest modules/session_example/tests/src/Functional/SessionExampleTest.php Tests the basic functions of the Session Example module.