@@ -127,25 +127,26 @@ Check out the Symfony config reference to learn more about the other available
127
127
Basic Usage
128
128
-----------
129
129
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
131
132
controllers if you type-hint an argument with
132
- :class: `Symfony\\ Component\\ HttpFoundation\\ Session \\ SessionInterface `::
133
+ :class: `Symfony\\ Component\\ HttpFoundation\\ RequestStack `::
133
134
134
- use Symfony\Component\HttpFoundation\Session\SessionInterface ;
135
+ use Symfony\Component\HttpFoundation\RequestStack ;
135
136
136
137
class SomeService
137
138
{
138
- private $session ;
139
+ private $requestStack ;
139
140
140
- public function __construct(SessionInterface $session )
141
+ public function __construct(RequestStack $requestStack )
141
142
{
142
- $this->session = $session ;
143
+ $this->requestStack = $requestStack ;
143
144
}
144
145
145
146
public function someMethod()
146
147
{
147
148
// 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');
149
150
150
151
// gets an attribute by name
151
152
$foo = $this->session->get('foo');
@@ -157,10 +158,10 @@ controllers if you type-hint an argument with
157
158
}
158
159
}
159
160
160
- .. tip ::
161
+ .. deprecated :: 5.3
161
162
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.
164
165
165
166
Stored attributes remain in the session for the remainder of that user's session.
166
167
By default, session attributes are key-value pairs managed with the
@@ -175,22 +176,44 @@ class.
175
176
If your application needs are complex, you may prefer to use
176
177
:ref: `namespaced session attributes <namespaced-attributes >` which are managed with the
177
178
: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 ``:
180
181
181
182
.. configuration-block ::
182
183
183
184
.. code-block :: yaml
184
185
185
186
# 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%'
190
197
191
198
session.namespacedattributebag :
192
199
class : Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag
193
200
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
+
194
217
.. _session-avoid-start :
195
218
196
219
Avoid Starting Sessions for Anonymous Users
0 commit comments