@@ -94,29 +94,12 @@ Session Workflow
94
94
Session Attributes
95
95
..................
96
96
97
- :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session::set `
98
- Sets an attribute by key.
97
+ The session attributes are stored internally in a "Bag", a PHP object that acts
98
+ like an array. They can be set, removed, checked, etc. using the methods
99
+ explained later in this article for the ``AttributeBagInterface `` class. See
100
+ :ref: `attribute-bag-interface `.
99
101
100
- :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session::get `
101
- Gets an attribute by key.
102
-
103
- :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session::all `
104
- Gets all attributes as an array of key => value.
105
-
106
- :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session::has `
107
- Returns true if the attribute exists.
108
-
109
- :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session::replace `
110
- Sets multiple attributes at once: takes a keyed array and sets each key => value pair.
111
-
112
- :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session::remove `
113
- Deletes an attribute by key.
114
-
115
- :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session::clear `
116
- Clear all attributes.
117
-
118
- The attributes are stored internally in a "Bag", a PHP object that acts like
119
- an array. A few methods exist for "Bag" management:
102
+ In addition, a few methods exist for "Bag" management:
120
103
121
104
:method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session::registerBag `
122
105
Registers a :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ SessionBagInterface `.
@@ -168,19 +151,65 @@ the following API which is intended mainly for internal purposes:
168
151
:method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ SessionBagInterface::getName `
169
152
Returns the name of the session bag.
170
153
154
+ .. _attribute-bag-interface :
155
+
171
156
Attributes
172
157
~~~~~~~~~~
173
158
174
159
The purpose of the bags implementing the :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface `
175
160
is to handle session attribute storage. This might include things like user ID,
176
- and remember me login settings or other user based state information.
161
+ and "Remember Me" login settings or other user based state information.
177
162
178
163
:class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBag `
179
164
This is the standard default implementation.
180
165
181
166
:class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ NamespacedAttributeBag `
182
167
This implementation allows for attributes to be stored in a structured namespace.
183
168
169
+ :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface `
170
+ has a simple API
171
+
172
+ :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::set `
173
+ Sets an attribute by name (``set('name', 'value') ``).
174
+
175
+ :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::get `
176
+ Gets an attribute by name (``get('name') ``) and can define a default
177
+ value when the attribute doesn't exist (``get('name', 'default_value') ``).
178
+
179
+ :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::all `
180
+ Gets all attributes as an associative array of ``name => value ``.
181
+
182
+ :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::has `
183
+ Returns ``true `` if the attribute exists.
184
+
185
+ :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::replace `
186
+ Sets multiple attributes at once using an associative array (``name => value ``).
187
+ If the attributes existed, they are replaced; if not, they are created.
188
+
189
+ :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::remove `
190
+ Deletes an attribute by name and returns its value.
191
+
192
+ :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::clear `
193
+ Deletes all attributes.
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
+
210
+ Namespaced Attributes
211
+ .....................
212
+
184
213
Any plain key-value storage system is limited in the extent to which
185
214
complex data can be stored since each key must be unique. You can achieve
186
215
namespacing by introducing a naming convention to the keys so different parts of
@@ -205,35 +234,13 @@ the array::
205
234
$session->set('tokens', $tokens);
206
235
207
236
With structured namespacing, the key can be translated to the array
208
- structure like this using a namespace character (defaults to ``/ ``)::
209
-
210
- $session->set('tokens/c', $value);
211
-
212
- This way you can easily access a key within the stored array directly and easily.
213
-
214
- :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface `
215
- has a simple API
216
-
217
- :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::set `
218
- Sets an attribute by key.
219
-
220
- :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::get `
221
- Gets an attribute by key.
222
-
223
- :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::all `
224
- Gets all attributes as an array of key => value.
225
-
226
- :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::has `
227
- Returns true if the attribute exists.
228
-
229
- :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::replace `
230
- Sets multiple attributes at once: takes a keyed array and sets each key => value pair.
237
+ structure like this using a namespace character (which defaults to ``/ ``)::
231
238
232
- :method: ` Symfony \\ Component \\ HttpFoundation \\ Session \\ Attribute \\ AttributeBagInterface::remove `
233
- Deletes an attribute by key.
239
+ // ...
240
+ use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
234
241
235
- :method: ` Symfony \\ Component \\ HttpFoundation \\ Session\\ Attribute \\ AttributeBagInterface::clear `
236
- Clear the bag.
242
+ $session = new Session(new NativeSessionStorage(), new NamespacedAttributeBag());
243
+ $session->set('tokens/c', $value);
237
244
238
245
Flash Messages
239
246
~~~~~~~~~~~~~~
0 commit comments