Skip to content

[HttpFoundation] Deprecate Request::getSession() when Request::hasSession() is false #9625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions components/http_foundation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
14 changes: 9 additions & 5 deletions security/impersonating_user.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down