@@ -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,12 +151,14 @@ 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.
@@ -185,25 +170,27 @@ and remember me login settings or other user based state information.
185
170
has a simple API
186
171
187
172
:method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::set `
188
- Sets an attribute by key .
173
+ Sets an attribute by name (`` set('name', 'value') ``) .
189
174
190
175
:method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::get `
191
- Gets an attribute by key.
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') ``).
192
178
193
179
:method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::all `
194
- Gets all attributes as an array of key => value.
180
+ Gets all attributes as an associative array of `` name => value`` .
195
181
196
182
:method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::has `
197
- Returns true if the attribute exists.
183
+ Returns `` true `` if the attribute exists.
198
184
199
185
:method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::replace `
200
- Sets multiple attributes at once: takes a keyed array and sets each key => value pair.
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.
201
188
202
189
:method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::remove `
203
- Deletes an attribute by key .
190
+ Deletes an attribute by name .
204
191
205
192
:method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBagInterface::clear `
206
- Clear the bag .
193
+ Deletes all attributes .
207
194
208
195
Namespaced Attributes
209
196
.....................
@@ -232,24 +219,21 @@ the array::
232
219
$session->set('tokens', $tokens);
233
220
234
221
With structured namespacing, the key can be translated to the array
235
- structure like this using a namespace character (defaults to ``/ ``)::
222
+ structure like this using a namespace character (which defaults to ``/ ``)::
236
223
237
224
$session->set('tokens/c', $value);
238
225
239
- This way you can easily access a key within the stored array directly and easily.
240
-
241
- To activate Namespaced Attributes, add this to your `services.yml `::
226
+ To activate namespaced attributes, add this to your ``services.yml ``::
242
227
243
228
# app/config/services.yml
244
-
245
229
services:
246
230
session:
247
231
class: Symfony\Component\HttpFoundation\Session\Session
248
232
arguments:
249
233
- @session.storage
250
- - @your .session.attribute_bag #service id is defined below
234
+ - @app .session.attribute_bag # this service id is defined below
251
235
- @session.flash_bag
252
- your .session.attribute_bag:
236
+ app .session.attribute_bag:
253
237
class: Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag
254
238
255
239
Flash Messages
0 commit comments