@@ -192,6 +192,21 @@ has a simple API
192
192
:method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::clear `
193
193
Deletes all attributes.
194
194
195
+ Example::
196
+
197
+ use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
198
+ use Symfony\Component\HttpFoundation\Session\Session;
199
+ use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
200
+
201
+ $session = new Session(new NativeSessionStorage(), new AttributeBag());
202
+ $session->set('token', 'a6c1e0b6');
203
+ // ...
204
+ $token = $session->get('token');
205
+ // if the attribute may or may not exist, you can define a default value for it
206
+ $token = $session->get('attribute-name', 'default-attribute-value');
207
+ // ...
208
+ $session->clear();
209
+
195
210
Namespaced Attributes
196
211
.....................
197
212
@@ -221,20 +236,11 @@ the array::
221
236
With structured namespacing, the key can be translated to the array
222
237
structure like this using a namespace character (which defaults to ``/ ``)::
223
238
224
- $session->set('tokens/c', $value);
239
+ // ...
240
+ use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
225
241
226
- To activate namespaced attributes, add this to your ``services.yml ``::
227
-
228
- # app/config/services.yml
229
- services:
230
- session:
231
- class: Symfony\Component\HttpFoundation\Session\Session
232
- arguments:
233
- - @session.storage
234
- - @app.session.attribute_bag # this service id is defined below
235
- - @session.flash_bag
236
- app.session.attribute_bag:
237
- class: Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag
242
+ $session = new Session(new NativeSessionStorage(), new NamespacedAttributeBag());
243
+ $session->set('tokens/c', $value);
238
244
239
245
Flash Messages
240
246
~~~~~~~~~~~~~~
0 commit comments