diff --git a/components/http_foundation.rst b/components/http_foundation.rst index 97d910e4ae0..d0d8963581b 100644 --- a/components/http_foundation.rst +++ b/components/http_foundation.rst @@ -235,6 +235,12 @@ the method tells you if the request contains a session which was started in one of the previous requests. +.. versionadded:: 4.1 + Using :method:`Symfony\\Component\\HttpFoundation\\Request::getSession()` + when no session has been set was deprecated in Symfony 4.1. It will throw + an exception in Symfony 5.0 when the session is ``null``. Check for an existing session + first by calling :method:`Symfony\\Component\\HttpFoundation\\Request::hasSession()`. + Accessing ``Accept-*`` Headers Data ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/security/impersonating_user.rst b/security/impersonating_user.rst index b4b70e8d9cd..6c1c6b99d36 100644 --- a/security/impersonating_user.rst +++ b/security/impersonating_user.rst @@ -209,11 +209,15 @@ you switch users, add an event subscriber on this event:: { public function onSwitchUser(SwitchUserEvent $event) { - $event->getRequest()->getSession()->set( - '_locale', - // assuming your User has some getLocale() method - $event->getTargetUser()->getLocale() - ); + $request = $event->getRequest(); + + if ($request->hasSession() && ($session = $request->getSession)) { + $session->set( + '_locale', + // assuming your User has some getLocale() method + $event->getTargetUser()->getLocale() + ); + } } public static function getSubscribedEvents()