3 namespace Drupal\session_test\Controller;
5 use Drupal\Core\Controller\ControllerBase;
6 use Symfony\Component\HttpFoundation\JsonResponse;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\Response;
11 * Controller providing page callbacks for the action admin interface.
13 class SessionTestController extends ControllerBase {
16 * Prints the stored session value to the screen.
19 * A notification message.
21 public function get() {
22 return empty($_SESSION['session_test_value'])
24 : ['#markup' => $this->t('The current value of the stored session variable is: %val', ['%val' => $_SESSION['session_test_value']])];
28 * Prints the stored session value to the screen.
30 * @param \Symfony\Component\HttpFoundation\Request $request
31 * The incoming request.
34 * A notification message.
36 public function getFromSessionObject(Request $request) {
37 $value = $request->getSession()->get("session_test_key");
40 : ['#markup' => $this->t('The current value of the stored session variable is: %val', ['%val' => $value])];
44 * Print the current session ID.
46 * @param \Symfony\Component\HttpFoundation\Request $request
47 * The incoming request.
50 * A notification message with session ID.
52 public function getId(Request $request) {
53 // Set a value in $_SESSION, so that SessionManager::save() will start
55 $_SESSION['test'] = 'test';
57 $request->getSession()->save();
59 return ['#markup' => 'session_id:' . session_id() . "\n"];
63 * Print the current session ID as read from the cookie.
65 * @param \Symfony\Component\HttpFoundation\Request $request
69 * A notification message with session ID.
71 public function getIdFromCookie(Request $request) {
72 return ['#markup' => 'session_id:' . $request->cookies->get(session_name()) . "\n", '#cache' => ['contexts' => ['cookies:' . session_name()]]];
76 * Stores a value in $_SESSION['session_test_value'].
78 * @param string $test_value
82 * A notification message.
84 public function set($test_value) {
85 $_SESSION['session_test_value'] = $test_value;
87 return ['#markup' => $this->t('The current value of the stored session variable has been set to %val', ['%val' => $test_value])];
91 * Turns off session saving and then tries to save a value
94 * @param string $test_value
98 * A notification message.
100 public function noSet($test_value) {
101 \Drupal::service('session_handler.write_safe')->setSessionWritable(FALSE);
102 $this->set($test_value);
103 return ['#markup' => $this->t('session saving was disabled, and then %val was set', ['%val' => $test_value])];
107 * Sets a message to me displayed on the following page.
110 * A notification message.
112 public function setMessage() {
113 $this->messenger()->addStatus($this->t('This is a dummy message.'));
114 return new Response($this->t('A message was set.'));
115 // Do not return anything, so the current request does not result in a themed
116 // page with messages. The message will be displayed in the following request
121 * Sets a message but call drupal_save_session(FALSE).
124 * A notification message.
126 public function setMessageButDontSave() {
127 \Drupal::service('session_handler.write_safe')->setSessionWritable(FALSE);
129 return ['#markup' => ''];
133 * Only available if current user is logged in.
136 * A notification message.
138 public function isLoggedIn() {
139 return ['#markup' => $this->t('User is logged in.')];
143 * Returns the trace recorded by test proxy session handlers as JSON.
145 * @param \Symfony\Component\HttpFoundation\Request $request
146 * The incoming request.
148 * @return \Symfony\Component\HttpFoundation\JsonResponse
151 public function traceHandler(Request $request) {
152 // Start a session if necessary, set a value and then save and close it.
153 $request->getSession()->start();
154 if (empty($_SESSION['trace-handler'])) {
155 $_SESSION['trace-handler'] = 1;
158 $_SESSION['trace-handler']++;
160 $request->getSession()->save();
162 // Collect traces and return them in JSON format.
163 $trace = \Drupal::service('session_test.session_handler_proxy_trace')->getArrayCopy();
165 return new JsonResponse($trace);
169 * Returns the values stored in the active session and the user ID.
171 * @param \Symfony\Component\HttpFoundation\Request $request
172 * The request object.
174 * @return \Symfony\Component\HttpFoundation\JsonResponse
175 * A response object containing the session values and the user ID.
177 public function getSession(Request $request) {
178 return new JsonResponse(['session' => $request->getSession()->all(), 'user' => $this->currentUser()->id()]);
182 * Sets a test value on the session.
184 * @param \Symfony\Component\HttpFoundation\Request $request
185 * The request object.
186 * @param string $test_value
187 * A value to set on the session.
189 * @return \Symfony\Component\HttpFoundation\JsonResponse
190 * A response object containing the session values and the user ID.
192 public function setSession(Request $request, $test_value) {
193 $session = $request->getSession();
194 $session->set('test_value', $test_value);
195 return new JsonResponse(['session' => $session->all(), 'user' => $this->currentUser()->id()]);