How do I store the values submitted in a form in the session? I want to submit a form and store the submitted values in the session. In Drupal 7 I just used $_SESSION['key'] = $value; but how do I correctly implement this in Drupal 8?
In D8 the session data is provided by a symfony session object, which is attached to the request. See this comparison of D7 against D8:

Drupal 7:

function mymodule_session_counter_increment() {
  if (!isset($_SESSION['mymodule_count'])) {
    $_SESSION['mymodule_count'] = 0;
  }

  return $_SESSION['mymodule_count']++;
}


Drupal 8:

class MymoduleSessionCounter {
  function increment(Request $request) {
    $session = $request-&gt;getSession();
    $value = $session-&gt;get('mymodule_count', 0);
    $session-&gt;set('mymodule_count', $value + 1);

    return $value;
  }
}


Change record: Access session data through the Request object

Session management has not changed. You don't need to do anything, because Drupal starts a session for each request automatically and you can use it right away.

Examples how to get the session:

Procedural

In procedural code get the request from the static wrapper \Drupal:

$request = \Drupal::request();
$session = $request-&gt;getSession();
$session-&gt;set('mymodule_value', $value);


Controller

In a controller you can pull the request from the route argument stack by including a typed request parameter. An example from UserController:

  public function resetPass(Request $request, $uid, $timestamp, $hash) {          
    ...
    $session = $request-&gt;getSession();
    $session-&gt;set('pass_reset_hash', $hash);
    $session-&gt;set('pass_reset_timeout', $timestamp);
    ...
  }


It's not necessary to define the route parameter in the route definition. The request is always available.

Form

In a form method get the request from getRequest():

  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
    $session = $this-&gt;getRequest()-&gt;getSession();
    $session-&gt;set('mymodule_form_value', $form_state-&gt;getValue('value'));
  }




Private Tempstore

The private tempstore is not a replacement for sessions. It depends on the session id and it's not possible to use it without a session. So you can store the values directly in the session as well. Drupal core uses the private tempstore for big amount of data and only for authenticated users who already have a session. For anonymous users it's not easy to get a private tempstore started, because drupal uses dummy session id's until it starts a real session. If you start a private tempstore in custom code it has the wrong session id.