@@ -192,6 +192,21 @@ has a simple API
192192:method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::clear `
193193 Deletes all attributes.
194194
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+
195210Namespaced Attributes
196211.....................
197212
@@ -221,20 +236,11 @@ the array::
221236With structured namespacing, the key can be translated to the array
222237structure like this using a namespace character (which defaults to ``/ ``)::
223238
224- $session->set('tokens/c', $value);
239+ // ...
240+ use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
225241
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);
238244
239245Flash Messages
240246~~~~~~~~~~~~~~
0 commit comments