Skip to content

[Session] "getFlashBag" interfacing issue #11279

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
linaori opened this issue Jul 3, 2014 · 5 comments
Closed

[Session] "getFlashBag" interfacing issue #11279

linaori opened this issue Jul 3, 2014 · 5 comments

Comments

@linaori
Copy link
Contributor

linaori commented Jul 3, 2014

When you want the FlashBag to set a message, you'd like to do getFlashBag on the session. However, you can only achieve this by type hinting an injection on the Session\Session object. The Session\SessionInterface doesn't contain the "getFlashBag" method thus you can't use the Request::getSession();

If you do want to get the flash bag from it, you can try "getSession()->getBag('flash bag name')". However, that doesn't guarantee that you get the flash bag that was inserted because you don't know the name.

It's a minor inconvenience that has multiple custom solutions, but I've seen this issue come around a couple of times already in "#symfony".

@jakzal
Copy link
Contributor

jakzal commented Jul 3, 2014

Duplicate of #10036 #5568. See @Drak's explanation here: #5568 (comment)

@linaori
Copy link
Contributor Author

linaori commented Jul 3, 2014

Correct, but they are closed while it's still an issue.

Possible solution? Create a FlashAwareSessionInterface that extends the SessionInterface but also has the getFlashBag for example.

Edit: getBag('flashes') isn't 100% safe either.

@linaori
Copy link
Contributor Author

linaori commented Jul 24, 2014

@fabpot you might possibly be interested, I've solved my problem like this:

services:
    session.flash_bag:
        class:           Symfony\Component\HttpFoundation\Session\FlashBagInterface
        factory_service: session
        factory_method:  getFlashBag

This has the following benefits:

  • You can inject it, even if you have no request/session available
  • You don't need to know the flashbag name to do Request::getSession()::getBag('flashes') as the name can change
  • If a different bag is used in the session, you won't use the wrong one by accident
  • You don't have to inject the Session in order to get the FlashBag

It might not really go well with: #11315 because it's an interface typehint. But that should be fixable.

This change should not have any BC issues,

@linaori
Copy link
Contributor Author

linaori commented Aug 3, 2015

Closing this issue as it seems like it's not a priority to fix as a work-around is available.

@linaori linaori closed this as completed Aug 3, 2015
@FrancescoBorzi
Copy link
Contributor

it looks like this is still an issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants