You are here

Example: Session in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 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
Session example module demonstrates manipulating user sessions.

Classes

Namesort descending Location Description
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.