Skip to content

Commit c145ed1

Browse files
pbowyerjaviereguiluz
authored andcommitted
Show how to change the Session Attribute Bag type
1 parent c784a48 commit c145ed1

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,6 @@ More about Sessions
201201
session/locale_sticky_session
202202
session/php_bridge
203203
session/proxy_examples
204+
session/attribute_bag
204205

205206
.. _`HttpFoundation component`: https://symfony.com/components/HttpFoundation

session/attribute_bag.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
.. index::
2+
single: Sessions, Attribute Bag
3+
4+
Changing the Session Attribute Bag Provider
5+
===========================================
6+
7+
Symfony uses attribute bags to represent values stored in the session. It ships
8+
with the default :class:`Symfony\\Component\\HttpFoundation\\Session\\Attribute\\AttributeBag`,
9+
which stores key-value pairs, and the
10+
:class:`Symfony\\Component\\HttpFoundation\\Session\\Attribute\\NamespacedAttributeBag`,
11+
which allows you to :ref:`set and fetch values using character-separated paths <namespaced-attributes>`.
12+
13+
This is fully documented :doc:`in the component documentation </components/http_foundation/sessions>`.
14+
15+
Changing the Attribute Bag in Symfony
16+
-------------------------------------
17+
18+
To use a different attribute bag for your sessions, override the service
19+
definition. In this example, we switch the default ``AttributeBag`` to the
20+
``NamespacedAttributeBag``:
21+
22+
.. configuration-block::
23+
24+
.. code-block:: yaml
25+
26+
# config/services.yaml
27+
session:
28+
class: Symfony\Component\HttpFoundation\Session\Session
29+
arguments: ["@session.storage", "@session.namespacedattributebag", "@session.flash_bag"]
30+
31+
session.namespacedattributebag:
32+
class: Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag
33+

0 commit comments

Comments
 (0)