14

I am trying to use symfony2 sessions.I do this

    $session = $this->getRequest()->getSession();
    $session->set('token','value');

This works. But i want to use namespace in session. Documentation says

    class NamespacedAttributeBag 

provides that feature but i cannot figure out how to implement it

6 Answers 6

15

Just open your config.yml and after imports add:

parameters:
    session.attribute_bag.class: Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag

It looks like this:

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }

parameters:
    session.attribute_bag.class: Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag

framework:
# ...
1
  • 2
    FYI: this won't work in Symfony 3, since they ripped out all of these class parameters. Commented May 16, 2016 at 2:10
9

You should redefine session service and also define service for your attribute bag (if you'll check default implementation of session.attribute_bag you'll see that this service has only class attribute).

And inject your new service to redefined session service into there

services:
    session:
        class: Symfony\Component\HttpFoundation\Session\Session
        arguments:
            - @session.storage
            - @your.session.attribute_bag #service id is defined below
            - @session.flash_bag

    your.session.attribute_bag:
        class: Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag
5
  • 3
    wow -- the docs really shouldn't even mention the NamespacedAttributeBag without going into more detail about how to use it. after banging my head against $session->registerBag() i'm glad to find this solution!
    – Casey
    Commented Oct 3, 2012 at 8:26
  • 1
    This helped alot but why isn`t the attribute consistant. I set some properties and next function call they are back to the default?
    – gprusiiski
    Commented Jan 13, 2014 at 10:44
  • You shouldn't redefine services like that. You should alter the container in a compiler pass or overwrite the parameter in config. Commented May 16, 2016 at 1:55
  • EDIT: This is one of the only ways to do this in Symfony 3...or use a compiler pass. Commented May 16, 2016 at 2:11
  • The doc is not clear about this, thanks I was totally stuck. I don't understand why it is not the default implementation
    – AlexisWbr
    Commented Jun 19, 2016 at 18:05
4

Because it's also possible to use the HTTPFoundation Component outside of Symfony2, the way to implement NamespacedUserBags is as follows:

use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;

$session = new Session();

//first bag
$myAttributeBag = new NamespacedAttributeBag('<your_storage_key_1>');
$myAttributeBag->setName('<your_tag_name_1>');
$session->registerBag($myAttributeBag);

//second bag
$myAttributeBag = new NamespacedAttributeBag('<your_storage_key_2>');
$myAttributeBag->setName('<your_tag_name_2>');
$session->registerBag($myAttributeBag);

$session->start();

Register as many bags as you want, but make sure to do this before you start the session. Now you can switch between bags using getBag():

$activeBag = $session->getBag('<your_tag_name>');

and access the namespaced bag with the typical methods :

$activeBag->set('tokens/a', 'adsf82983asd');
$activeBag->set('tokens/b', 'daslfl232l3k');

print_r($activeBag->get('tokens'));
2

Since Symfony 3, the override of session.attribute_bag.class parameter doesn't work anymore.

The solution I applied after pulling my hair for a few time is using a compiler pass to override the session.attribute_bag service class.

I did it in the Kernel directly, but an external compiler pass would work the same way.

SF4 Kernel

<?php
// src/Kernel.php
namespace App;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;

class Kernel extends BaseKernel implements CompilerPassInterface
{
    use MicroKernelTrait;

    // ...

    public function process(ContainerBuilder $container)
    {
        $container->getDefinition('session.attribute_bag')->setClass(NamespacedAttributeBag::class);
    }
}
1

With Symfony 4 (and Flex), use the following configuration to use NamespacedAttributeBag:

# config/services.yaml
services:
  session.attribute_bag: 
    class: Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag
  # ...
0

Update: Namespaced Sessions were removed in Symfony 6.0

See https://symfony.com/doc/5.4/session.html#basic-usage

The NamespacedAttributeBag class is deprecated since Symfony 5.3. If you need this feature, you will have to implement the class yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.