8000 [HttpFoundation] Deprecate Request::getSession() when Request::hasSes… · symfony/symfony-docs@b8f7627 · GitHub
[go: up one dir, main page]

Skip to content

Commit b8f7627

Browse files
Henry Snoekjaviereguiluz
Henry Snoek
authored andcommitted
[HttpFoundation] Deprecate Request::getSession() when Request::hasSession() is false
1 parent 001f5dd commit b8f7627

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

components/http_foundation.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ the
235235
method tells you if the request contains a session which was started in one of
236236
the previous requests.
237237

238+
.. versionadded:: 4.1
239+
Using :method:`Symfony\\Component\\HttpFoundation\\Request::getSession()`
240+
when no session has been set was deprecated in Symfony 4.1. It will throw
241+
an exception in Symfony 5.0 when the session is ``null``. Check for an existing session
242+
first by calling :method:`Symfony\\Component\\HttpFoundation\\Request::hasSession()`.
243+
238244
Accessing ``Accept-*`` Headers Data
239245
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
240246

security/impersonating_user.rst

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,15 @@ you switch users, add an event subscriber on this event::
209209
{
210210
public function onSwitchUser(SwitchUserEvent $event)
211211
{
212-
$event->getRequest()->getSession()->set(
213-
'_locale',
214-
// assuming your User has some getLocale() method
215-
$event->getTargetUser()->getLocale()
216-
);
212+
$request = $event->getRequest();
213+
214+
if ($request->hasSession() && ($session = $request->getSession)) {
215+
$session->set(
216+
'_locale',
217+
// assuming your User has some getLocale() method
218+
$event->getTargetUser()->getLocale()
219+
);
220+
}
217221
}
218222

219223
public static function getSubscribedEvents()

0 commit comments

Comments
 (0)
0