-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
No longer assume session service has a getFlashBag method #32387
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
Conversation
In runtime it usually does, because the symfony Session class has that method, but it's not on the SessionInterface, so we must not assume it exists. Calling it breaks custom implementation of the SessionInterface that do not have the getFlashBag method.
should we add it using |
That was what I wanted to propose initially, but it seems it was decided against that. See #5568 (comment) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
im still skeptical about method_exists
, that's not a contract.
What about an internal utility wrapper to get us a flashbag from a SessionInterface (FlashBag::createFromSession
). Then we could additionally try getBag('flashes')
by convention for a fallback.
Yes, I also don't like the duck typing here. But it's the most robust way I see at the moment for a bugfix release. To improve this, we could introduce a new interface that provides
The assumption that the flash bag is stored as |
Right, i wasnt aware the PR targets 3.4 :) But i dont find it robust without any additional type check for
Not if we type check as such :) Im thinking function getMeAFlashBag(SessionInterface $session): ?FlashBagInterface
{
if instanceof Session; return getFlashBag()
if getBag(flashes) instanceof FlashBagInterface; return getBag('flashes')
return null
}
That wont work for 3.4 since it's not final. So the drawback is we need to add an internal new symbol in 3.4. But it's the robust way IMHO that remains viable for now without feature creeping it. |
That'd break if I had a session like this one: class MyOwnSession implements SessionInterface
{
//…
public function getFlashBag(): FlashBagInterface
{
return $this->getBag('my_own_flashes');
}
} |
Fair :) 3.4 needs a patch either way. For 4.4 we could let the framework configure the intended bag name (e.g. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got one minor comment, looks like a good change overall!
src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php
Outdated
Show resolved
Hide resolved
Also fix return type for AppVariable::getSession(), which at runtime would indeed mostly return a Session instance, but not necessarily
My point here was that the use of
I feel like fixing the type system would be the more sustainable solution in the long run, i.e., the |
IMHO the only truth is symfony/src/Symfony/Component/HttpFoundation/Session/Session.php Lines 261 to 266 in 6811aaa
so if anything, we expect a I agree with #5568 (comment), we don't need it in a contract per se, having the bag name is sufficient. |
I don't think the flash bag method will ever be added to the interface: #20258 (comment) |
But what if people are implementing a SaaS and have multiple flash bags, one per tenant? |
Not sure how this would be something per tenant, as multi tenancy is usually not recommended at application level |
Fair enough. Just meant that the fact that there is one flash bag is also an assumption, and there are reasons there might be multiple, with different names. |
No, but we can introduce a separate interface for that method and end the duck typing with 5.0. |
Wouldn't a completely separate interface with a separate implementation for flash messages be nicer. as opposed to retro-fitting an interface to a flawed design? If we add an interface for So how about something like
(I'm not stuck on the name, feel free to provide a better name!) And then the default implementation could be injected a
The Later on the methods of the So then it is using the session to store and read flash messages, but the session itself has no idea this is happening. Which, going by the current implementation, is what we wanted in the first place. Also, if people want to store the messages elsewhere / have multiple FlashMessageBags / whatever, they are free to implement their own Additionaly, as a migration path, To return the flash bag from Do you think this would be viable? If so, I'll work it out some more and submit an RFC. |
@rpkamp The interface that you’re describing is the |
The interface is indeed the same, but it puts it top level, disconnected from the session (yes, they are connected by implementation, but not by concept). The implementation right now is in the Also please note that if we introduce such a The user land code will be nicer though (IMO):
as apposed to
(which violates the law of Demeter BTW) On the other hand, people will mostly use the |
There's work in progress to remove the session from the container, because it's an object bound to http context. If a separate object would be introduced for flashes, it should probably not be used as service. |
The flash bag is already a standalone service. You don't have to take the detour via the session. Also, if you directly use the flash bag, your userland code does not need to care that the session is used to persist the flashes. class MyService
{
public function __construct(FlashBagInterface $flashBag)
{
$this->flashBag = $flashBag;
}
public function doSometing()
{
// …
$this->flashBag->add('notice', 'Something happened');
// …
}
} |
What about removing access to the flash bag from the |
@fabpot that would introduce the same coupling as to the Seeing the Request is usually the point of entry for events or otherwise available in the majority of code and passed along as request related state, perhaps this should be a responsibility of the |
At least it would loose it's dubious state of being implemented in the Also, moving the session to the
When the flash messages are moved to the |
I think #10557 is the ticket for this discussion. :-) |
@rpkamp The session is already present: |
Closing: this staled, and doesn't solve a real-world use case (or at least it's not clear which one it solves.) |
When using a form authenticator (AbstractFormLoginAuthenticator), the method "onAuthenticationSuccess" output a redirection. I would like to display a message of success for the user who successfully logged in. The use of the flashbag from the authenticator would have been nice for that use case. Do you have advise to solve this use case? |
Sounds like something unrelated from this ticket @Saryon61. I would recommend you open a new ticket with your question. |
In runtime the
sessions
service usually has agetFlashBag
method, because the symfony Session class has that method, but it's not on the SessionInterface, so we must not assume it exists.Calling it breaks custom implementation of the SessionInterface that do not have the getFlashBag method.