aditya
aditya

Reputation: 996

How to use Namespaced Sessions in Symfony2

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

Upvotes: 14

Views: 5388

Answers (6)

Thomas Landauer
Thomas Landauer

Reputation: 8355

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.

Upvotes: 0

Kévin Dunglas
Kévin Dunglas

Reputation: 3024

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
  # ...

Upvotes: 1

Kern
Kern

Reputation: 868

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);
    }
}

Upvotes: 2

Ch3rm
Ch3rm

Reputation: 51

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'));

Upvotes: 4

Roman Shamritskiy
Roman Shamritskiy

Reputation: 753

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:
# ...

Upvotes: 15

Vitalii Zurian
Vitalii Zurian

Reputation: 17976

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

Upvotes: 9

Related Questions