I wrote a controller in Shopware 6 (symfony based) that adds a flash messages via $this->addFlash(...)
.
As expected the message is shown in the storefront (product detail page).
In the function SymfonyComponentHttpFoundationSessionFlashFlashBag::all
the message is removed as expected (checked via xdebug).
But when I refresh, the flash message is shown again and is also contained in the session.
I also checked this directly in the PHP session:
$_SESSION['_symfony_flashes']
The message is correctly removed there, but reappears on the next page load.
The only idea I have is, that the session is not persisted after the flash message is removed from it. But I never faced such behavior in the Symfony sessions or PHP sessions.
What could be the reason for this?
2
Answers
My best guess is that the page is cached with the flash message intact. Afaik flash messages are primarily used on routes that are exempt from caching. The product detail page is not one of those by default. You might have to enrich the cache key for the page by a parameter denoting whether the flash message should be shown or not.
Adding to dneustadt’s answer: As the product detail page is cached, also your flash message will not show if the detail page is generated and cached without the
addFlash()
and later your condition for theaddFlash()
becomes true: Then your controller will just not be hit and the cached page without the flash message will be shown.