Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Namespaced Sessions in Symfony2

Tags:

php

symfony

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

like image 969
aditya Avatar asked Sep 05 '12 10:09

aditya


2 Answers

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:
# ...
like image 183
Roman Shamritskiy Avatar answered Nov 16 '22 23:11

Roman Shamritskiy


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
like image 9
Vitalii Zurian Avatar answered Nov 17 '22 00:11

Vitalii Zurian