You are here

public function SessionExampleController::showSession in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/session_example/src/Controller/SessionExampleController.php \Drupal\session_example\Controller\SessionExampleController::showSession()

Display the example session information.

By default, controller methods receive a Request object as a parameter, so we can use one here.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request object.

Return value

string A renderable array containing the user information from the session.

1 string reference to 'SessionExampleController::showSession'
session_example.routing.yml in session_example/session_example.routing.yml
session_example/session_example.routing.yml

File

session_example/src/Controller/SessionExampleController.php, line 27

Class

SessionExampleController
Controller for a page to display the session information.

Namespace

Drupal\session_example\Controller

Code

public function showSession(Request $request) {

  // Get the session from the request object.
  $session = $request
    ->getSession();

  // Make a table of the session information.
  $row = [];
  foreach ([
    'name',
    'email',
    'quest',
    'color',
  ] as $item) {
    $key = "session_example.{$item}";

    // Get the session value, with a default of 'No name' etc. for each type
    // of information we have.
    $row[0][$item] = $session
      ->get($key, $this
      ->t('No @type', [
      '@type' => $item,
    ]));
  }
  return [
    // Since this page will be cached, we have to manage the caching. We'll
    // use a cache tag and manage it within the session helper. We use the
    // session ID to guarantee a unique tag per session. The submission form
    // will manage invalidating this tag.
    '#cache' => [
      'tags' => [
        'session_example:' . $session
          ->getId(),
      ],
    ],
    'description' => [
      '#type' => 'item',
      '#title' => $this
        ->t('Saved Session Keys'),
      '#markup' => $this
        ->t('The example form lets you set some session keys.  This page lists their current values.'),
    ],
    'session_status' => [
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Name'),
        $this
          ->t('Email'),
        $this
          ->t('Quest'),
        $this
          ->t('Color'),
      ],
      '#rows' => $row,
    ],
  ];
}