Skip to content

Commit b6672e4

Browse files
committed
minor #14898 [FrameworkBundle] Update documentation for deprecated session service (jderusse)
This PR was merged into the 5.3-dev branch. Discussion ---------- [FrameworkBundle] Update documentation for deprecated session service fixes #14896 Commits ------- be512a8 Update documentation for deprecated session service
2 parents b1547ff + be512a8 commit b6672e4

File tree

9 files changed

+64
-57
lines changed

9 files changed

+64
-57
lines changed

components/http_foundation.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ Accessing the Session
247247
~~~~~~~~~~~~~~~~~~~~~
248248

249249
If you have a session attached to the request, you can access it via the
250-
:method:`Symfony\\Component\\HttpFoundation\\Request::getSession` method;
250+
:method:`Symfony\\Component\\HttpFoundation\\Request::getSession` method or the
251+
:method:`Symfony\\Component\\HttpFoundation\\RequestStack::getSession` method;
251252
the
252253
:method:`Symfony\\Component\\HttpFoundation\\Request::hasPreviousSession`
253254
method tells you if the request contains a session which was started in one of

controller.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ Request object.
394394
Managing the Session
395395
--------------------
396396

397-
Symfony provides a session service that you can use to store information
397+
Symfony provides a session object that you can use to store information
398398
about the user between requests. Session is enabled by default, but will only be
399399
started if you read or write from it.
400400

logging/processors.rst

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,33 @@ using a processor::
1919
// src/Logger/SessionRequestProcessor.php
2020
namespace App\Logger;
2121

22-
use Symfony\Component\HttpFoundation\Session\SessionInterface;
22+
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
23+
use Symfony\Component\HttpFoundation\RequestStack;
2324

2425
class SessionRequestProcessor
2526
{
26-
private $session;
27-
private $sessionId;
27+
private $requestStack;
2828

29-
public function __construct(SessionInterface $session)
29+
public function __construct(RequestStack $requestStack)
3030
{
31-
$this->session = $session;
31+
$this->requestStack = $requestStack;
3232
}
3333

3434
// this method is called for each log record; optimize it to not hurt performance
3535
public function __invoke(array $record)
3636
{
37-
if (!$this->session->isStarted()) {
37+
try {
38+
$session = $requestStack->getSession();
39+
} catch (SessionNotFoundException $e) {
40+
return;
41+
}
42+
if (!$session->isStarted()) {
3843
return $record;
3944
}
4045

41-
if (!$this->sessionId) {
42-
$this->sessionId = substr($this->session->getId(), 0, 8) ?: '????????';
43-
}
46+
$sessionId = substr($session->getId(), 0, 8) ?: '????????';
4447

45-
$record['extra']['token'] = $this->sessionId.'-'.substr(uniqid('', true), -8);
48+
$record['extra']['token'] = $sessionId.'-'.substr(uniqid('', true), -8);
4649

4750
return $record;
4851
}

quick_tour/the_architecture.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ What other possible classes or interfaces could you use? Find out by running:
7272
Request stack that controls the lifecycle of requests.
7373
Symfony\Component\HttpFoundation\RequestStack (request_stack)
7474
75-
Interface for the session.
76-
Symfony\Component\HttpFoundation\Session\SessionInterface (session)
77-
7875
RouterInterface is the interface that all Router classes must implement.
7976
Symfony\Component\Routing\RouterInterface (router.default)
8077

security/access_denied_handler.rst

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,23 @@ unauthenticated user tries to access a protected resource::
2828

2929
use Symfony\Component\HttpFoundation\RedirectResponse;
3030
use Symfony\Component\HttpFoundation\Request;
31-
use Symfony\Component\HttpFoundation\Session\SessionInterface;
3231
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
3332
use Symfony\Component\Security\Core\Exception\AuthenticationException;
3433
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
3534

3635
class AuthenticationEntryPoint implements AuthenticationEntryPointInterface
3736
{
3837
private $urlGenerator;
39-
private $session;
4038

41-
public function __construct(UrlGeneratorInterface $urlGenerator, SessionInterface $session)
39+
public function __construct(UrlGeneratorInterface $urlGenerator)
4240
{
4341
$this->urlGenerator = $urlGenerator;
44-
$this->session = $session;
4542
}
4643

4744
public function start(Request $request, AuthenticationException $authException = null): RedirectResponse
4845
{
4946
// add a custom flash message and redirect to the login page
50-
$this->session->getFlashBag()->add('note', 'You have to login in order to access this page.');
47+
$request->getSession()->getFlashBag()->add('note', 'You have to login in order to access this page.');
5148

5249
return new RedirectResponse($this->urlGenerator->generate('security_login'));
5350
}

security/form_login_setup.rst

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ whenever the user browses a page::
477477
namespace App\EventSubscriber;
478478

479479
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
480-
use Symfony\Component\HttpFoundation\Session\SessionInterface;
481480
use Symfony\Component\HttpKernel\Event\RequestEvent;
482481
use Symfony\Component\HttpKernel\KernelEvents;
483482
use Symfony\Component\Security\Http\Util\TargetPathTrait;
@@ -486,13 +485,6 @@ whenever the user browses a page::
486485
{
487486
use TargetPathTrait;
488487

489-
private $session;
490-
491-
public function __construct(SessionInterface $session)
492-
{
493-
$this->session = $session;
494-
}
495-
496488
public function onKernelRequest(RequestEvent $event): void
497489
{
498490
$request = $event->getRequest();
@@ -504,7 +496,7 @@ whenever the user browses a page::
504496
return;
505497
}
506498

507-
$this->saveTargetPath($this->session, 'main', $request->getUri());
499+
$this->saveTargetPath($request->getSession(), 'main', $request->getUri());
508500
}
509501

510502
public static function getSubscribedEvents()

service_container.rst

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ What other services are available? Find out by running:
6262
Request stack that controls the lifecycle of requests.
6363
Symfony\Component\HttpFoundation\RequestStack (request_stack)
6464
65-
Interface for the session.
66-
Symfony\Component\HttpFoundation\Session\SessionInterface (session)
67-
6865
RouterInterface is the interface that all Router classes must implement.
6966
Symfony\Component\Routing\RouterInterface (router.default)
7067
@@ -80,7 +77,7 @@ in the container.
8077
.. tip::
8178

8279
There are actually *many* more services in the container, and each service has
83-
a unique id in the container, like ``session`` or ``router.default``. For a full
80+
a unique id in the container, like ``request_stack`` or ``router.default``. For a full
8481
list, you can run ``php bin/console debug:container``. But most of the time,
8582
you won't need to worry about this. See :ref:`services-wire-specific-service`.
8683
See :doc:`/service_container/debug`.
@@ -283,9 +280,6 @@ type-hints by running:
283280
Request stack that controls the lifecycle of requests.
284281
Symfony\Component\HttpFoundation\RequestStack (request_stack)
285282
286-
Interface for the session.
287-
Symfony\Component\HttpFoundation\Session\SessionInterface (session)
288-
289283
RouterInterface is the interface that all Router classes must implement.
290284
Symfony\Component\Routing\RouterInterface (router.default)
291285

session.rst

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -127,25 +127,26 @@ Check out the Symfony config reference to learn more about the other available
127127
Basic Usage
128128
-----------
129129

130-
Symfony provides a session service that is injected in your services and
130+
The sessions is available througth the Request and the RequestStack.
131+
Symfony provides a request_stack service that is injected in your services and
131132
controllers if you type-hint an argument with
132-
:class:`Symfony\\Component\\HttpFoundation\\Session\\SessionInterface`::
133+
:class:`Symfony\\Component\\HttpFoundation\\RequestStack`::
133134

134-
use Symfony\Component\HttpFoundation\Session\SessionInterface;
135+
use Symfony\Component\HttpFoundation\RequestStack;
135136

136137
class SomeService
137138
{
138-
private $session;
139+
private $requestStack;
139140

140-
public function __construct(SessionInterface $session)
141+
public function __construct(RequestStack $requestStack)
141142
{
142-
$this->session = $session;
143+
$this->requestStack = $requestStack;
143144
}
144145

145146
public function someMethod()
146147
{
147148
// stores an attribute in the session for later reuse
148-
$this->session->set('attribute-name', 'attribute-value');
149+
$this->requestStack->getSession()->set('attribute-name', 'attribute-value');
149150

150151
// gets an attribute by name
151152
$foo = $this->session->get('foo');
@@ -157,10 +158,10 @@ controllers if you type-hint an argument with
157158
}
158159
}
159160

160-
.. tip::
161+
.. deprecated:: 5.3
161162

162-
Every ``SessionInterface`` implementation is supported. If you have your
163-
own implementation, type-hint this in the argument instead.
163+
The ``SessionInterface`` and ``session`` service are deprecated since
164+
Symfony 5.3. Inject a request stack instead.
164165

165166
Stored attributes remain in the session for the remainder of that user's session.
166167
By default, session attributes are key-value pairs managed with the
@@ -175,22 +176,44 @@ class.
175176
If your application needs are complex, you may prefer to use
176177
:ref:`namespaced session attributes <namespaced-attributes>` which are managed with the
177178
:class:`Symfony\\Component\\HttpFoundation\\Session\\Attribute\\NamespacedAttributeBag`
178-
class. Before using them, override the ``session`` service definition to replace
179-
the default ``AttributeBag`` by the ``NamespacedAttributeBag``:
179+
class. Before using them, override the ``session_listener`` service definition to build
180+
your ``Session`` object with the default ``AttributeBag`` by the ``NamespacedAttributeBag``:
180181

181182
.. configuration-block::
182183

183184
.. code-block:: yaml
184185
185186
# config/services.yaml
186-
session:
187-
public: true
188-
class: Symfony\Component\HttpFoundation\Session\Session
189-
arguments: ['@session.storage', '@session.namespacedattributebag']
187+
session_listener:
188+
autoconfigure: true
189+
class: App\EventListener\SessionListener
190+
arguments:
191+
- !service_locator
192+
logger: '@?logger'
193+
session_collector: '@?data_collector.request.session_collector'
194+
session_storage: '@session.storage'
195+
session_attributes: '@session.namespacedattributebag'
196+
- '%kernel.debug%'
190197
191198
session.namespacedattributebag:
192199
class: Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag
193200
201+
.. code-block:: php
202+
203+
namespace App\EventListener;
204+
205+
use Symfony\Component\HttpFoundation\Session\Session;
206+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
207+
use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
208+
209+
class SessionListener extends AbstractSessionListener
210+
{
211+
protected function getSession(): ?SessionInterface
212+
{
213+
return new Session($this->container->get('session_storage'), $this->container->get('session_attributes'));
214+
}
215+
}
216+
194217
.. _session-avoid-start:
195218

196219
Avoid Starting Sessions for Anonymous Users

session/locale_sticky_session.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ event::
146146
namespace App\EventSubscriber;
147147

148148
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
149-
use Symfony\Component\HttpFoundation\Session\SessionInterface;
149+
use Symfony\Component\HttpFoundation\RequestStack;
150150
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
151151
use Symfony\Component\Security\Http\SecurityEvents;
152152

@@ -156,19 +156,19 @@ event::
156156
*/
157157
class UserLocaleSubscriber implements EventSubscriberInterface
158158
{
159-
private $session;
159+
private $requestStack;
160160

161-
public function __construct(SessionInterface $session)
161+
public function __construct(RequestStack $requestStack)
162162
{
163-
$this->session = $session;
163+
$this->requestStack = $requestStack;
164164
}
165165

166166
public function onInteractiveLogin(InteractiveLoginEvent $event)
167167
{
168168
$user = $event->getAuthenticationToken()->getUser();
169169

170170
if (null !== $user->getLocale()) {
171-
$this->session->set('_locale', $user->getLocale());
171+
$this->requestStack->getSession()->set('_locale', $user->getLocale());
172172
}
173173
}
174174

0 commit comments

Comments
 (0)