Symfony cache is breaking my flash messages
Flash messages in Symfony projects give feedback to website users after an action is performed. They usually only show for one page load after the action is triggered.
Unfortunately, when caching is turned on my messages fail to appear and only the cached version of the page is returned.
In one instance a version of a p开发者_如何学JAVAage with a message was cached, meaning that the message was displayed to everyone.
Is it possible to make the cache aware of the flash messages?
There are two options, the one mentioned by benlumley:
- Turn with_layout off, so that the layout is not part of the cached file. This will not prevent your problem is your rendering of the flash is inside the action template though.
- Clear selected parts of the cache whenever you set and display the flash (remember to clear it after the display of the cache, otherwise you'll bump into your problem with the flash appearing to other users again. Alternatively you can choose to programmatically disable the cache once on the page that displays the flash (see the symfony docs on 'Configuring the Cache Dynamically). The last option is quite dodgy in my opinion and should be used as a last resort, since for example redirects can screw it all up.
No matter how you do it there is no optimal solution that can make your flash messages magically bypass the cache.
There is not automatic way to make symfony cope with this.
The best/most common solution is to disable with_layout for either all of your cached actions, or else just these particular actions where flash messages have to be displayed. This normally resolves the issue as most people tend to output flash messages from the layout rather than within the cached templates.
精彩评论