Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Registering new session bag does not initialize it #10707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
imunhatep opened this issue Apr 14, 2014 · 7 comments
Closed

Registering new session bag does not initialize it #10707

imunhatep opened this issue Apr 14, 2014 · 7 comments
Labels
DX DX = Developer eXperience (anything that improves the experience of using Symfony) HttpFoundation

Comments

@imunhatep
Copy link

If the session is already loaded:

Currently session handlers allow dynamically register new attribute bags, but does not initialize them on registering. And does not throw any exception that created session attribute bag is not saving any data to session.

Developer have to define custom session service and register all bags there (before session loads) or initialize bags by hands.

Symfony 2.4.2

@sstok
Copy link
Contributor

sstok commented Apr 23, 2014

Registering a new Bag (before) starting the session can be done using a CompilerPass.
https://github.com/rollerworks/RollerworksCacheBundle/blob/master/src/Rollerworks/Bundle/CacheBundle/DependencyInjection/Compiler/SessionPass.php Shows this for example.

But initializing a bag automatically on registering when the session is already started should indeed be possible.

/cc @Drak

@ghost
Copy link

ghost commented Apr 23, 2014

@imunhatep What you are suggesting is a code smell.

Bags are initialized only when the session is started so you must instantiate your session objects as you require before hand which can be done as suggested as @sstok suggests or just by altering the initial service definitions.

Remember, bags are not aware of the session so any attempt to make bags do things to the session would mean ugly dependencies which is another clear code smell.

@imunhatep
Copy link
Author

@Drak Where did you found any suggestion from me? I just sad it's not obvious that dynamically created bag is not wired to session! And looking at the code, I do not see any limitation to do so, or at least throw an exception.

@sstok thanks I definitely will take a look at this approach.

@jperovic
Copy link
Contributor

I agree with @imunhatep

@Drak Indeed bags are not aware of the session neither they should be, but since the bag registration is invoked from Session's side (Session::registerBag) that should be much possible.

@sstok This approach looks very nice :)

@ajanssens
Copy link

Just fell into this pitfall. I used Session::registerBag then didn't found my bad after a page change or a redirect. I must +1 this as it should register then initialize it... ! Or at least point out that it's not the only thing required to register a bag.

@digilist
Copy link
Contributor

I just ran into this issue, too. It was really annoying, to understand that the session has to be started after the bag was added. Current, I see two options to fix this:

  1. Call the initialize() method of the SessionBag from within the registerBag() method of the SessionStorage if the session was already started (similar to the code in loadSession()). Something like this:

        public function registerBag(SessionBagInterface $bag)
        {
            $this->bags[$bag->getName()] = $bag;
    
            if ($this->isStarted()) {
                $session = &$_SESSION;
                $key = $bag->getStorageKey();
                $session[$key] = isset($session[$key]) ? $session[$key] : array();
                $bag->initialize($session[$key]);
            }
        }

    The loadSession() methods accepts an optional session variable. I do not see how and where this parameter is used, so I am not sure what to do here.

  2. Forbid adding a bag after the session was started. In this case, an exception should be thrown and the documentation needs to be updated.

I'd prefer the first way. What do you think? I can provide a fix for this, if you agree.

@javiereguiluz javiereguiluz added the DX DX = Developer eXperience (anything that improves the experience of using Symfony) label Jan 27, 2016
@fabpot
Copy link
Member

fabpot commented Mar 2, 2016

I think we should go with option 2.

fabpot added a commit that referenced this issue Mar 8, 2016
…ed sessions (xabbuh)

This PR was merged into the 2.3 branch.

Discussion
----------

[HttpFoundation] exception when registering bags for started sessions

| Q             | A
| ------------- | ---
| Branch        | 2.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #10707, #16136
| License       | MIT
| Doc PR        |

Commits
-------

c4a5b67 exception when registering bags for started sessions
@fabpot fabpot closed this as completed Mar 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DX DX = Developer eXperience (anything that improves the experience of using Symfony) HttpFoundation
Projects
None yet
Development

No branches or pull requests

7 participants