Skip to content

Commit 4063283

Browse files
committed
minor #10495 Show how to change the Session Attribute Bag type (pbowyer)
This PR was submitted for the master branch but it was squashed and merged into the 4.2 branch instead (closes #10495). Discussion ---------- Show how to change the Session Attribute Bag type The Session component ships with two different attribute bag implementations, and the Symfony docs don't show how to change between them. Fixes #10494 and StackOverflow issues like https://stackoverflow.com/questions/12279247/how-to-use-namespaced-sessions-in-symfony2 ## Specific feedback requests 1. Is this the right location for it? Adding it to https://github.com/symfony/symfony-docs/blob/master/session.rst was my first idea, but everything else is sub-pages. 2. I only use YAML configuration, so if someone else can contribute/check the PHP/XML config that would be brilliant. 3. Hard line wrapping - you appear to use ~80 characters? I've got some lines that go over that but don't think paths to files can be split? Commits ------- c145ed1 Show how to change the Session Attribute Bag type
2 parents c784a48 + c145ed1 commit 4063283

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)