Skip to content

Commit 683cc5f

Browse files
committed
Merge branch '4.2'
* 4.2: Rewords and reorganization Show how to change the Session Attribute Bag type
2 parents 8112be5 + de23479 commit 683cc5f

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

components/http_foundation/sessions.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ Example::
207207
// ...
208208
$session->clear();
209209

210+
.. _namespaced-attributes:
211+
210212
Namespaced Attributes
211213
.....................
212214

session.rst

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,41 @@ controllers if you type-hint an argument with
151151
// gets an attribute by name
152152
$foo = $session->get('foo');
153153

154-
// uses a default value if the attribute doesn't exist
154+
// the second argument is the value returned when the attribute doesn't exist
155155
$filters = $session->get('filters', []);
156156

157157
// ...
158158
}
159159
}
160160

161-
Stored attributes remain in the session for the remainder of that user's session.
162-
163161
.. tip::
164162

165163
Every ``SessionInterface`` implementation is supported. If you have your
166164
own implementation, type-hint this in the argument instead.
167165

166+
Stored attributes remain in the session for the remainder of that user's session.
167+
By default, session attributes are key-value pairs managed with the
168+
:class:`Symfony\\Component\\HttpFoundation\\Session\\Attribute\\AttributeBag`
169+
class.
170+
171+
If your application needs are complex, you may prefer to use
172+
:ref:`namespaced session attributes <namespaced-attributes>` which are managed with the
173+
:class:`Symfony\\Component\\HttpFoundation\\Session\\Attribute\\NamespacedAttributeBag`
174+
class. Before using them, override the ``session`` service definition to replace
175+
the default ``AttributeBag`` by the ``NamespacedAttributeBag``:
176+
177+
.. configuration-block::
178+
179+
.. code-block:: yaml
180+
181+
# config/services.yaml
182+
session:
183+
class: Symfony\Component\HttpFoundation\Session\Session
184+
arguments: ['@session.storage', '@session.namespacedattributebag', '@session.flash_bag']
185+
186+
session.namespacedattributebag:
187+
class: Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag
188+
168189
.. _session-avoid-start:
169190

170191
Avoid Starting Sessions for Anonymous Users

0 commit comments

Comments
 (0)