@@ -127,25 +127,26 @@ Check out the Symfony config reference to learn more about the other available
127127Basic 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
131132controllers 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
165166Stored attributes remain in the session for the remainder of that user's session.
166167By default, session attributes are key-value pairs managed with the
@@ -175,22 +176,44 @@ class.
175176If 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
196219Avoid Starting Sessions for Anonymous Users
0 commit comments