Skip to content

Commit c164d94

Browse files
committed
bug #859 Fix session starting issue on flash messages usage (andrerom)
This PR was merged into the master branch. Discussion ---------- Fix session starting issue on flash messages usage Use of flash messages triggers session start, which as of last year triggers Symfony to mark response as private, and not cached. Fix the issue by checking if there is a session first as advised on: https://symfony.com/doc/current/session/avoid_session_start.html Commits ------- 2362ac8 Fix session starting issue in symfony-demo on flash messages
2 parents 4c9ea94 + 2362ac8 commit c164d94

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

templates/default/_flash_messages.html.twig

+20-12
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,27 @@
55
A common practice to better distinguish between templates and fragments is to
66
prefix fragments with an underscore. That's why this template is called
77
'_flash_messages.html.twig' instead of 'flash_messages.html.twig'
8+
9+
We check if we have session before reading flashes as it otherwise triggers session start:
10+
https://symfony.com/doc/current/session/avoid_session_start.html
11+
12+
TIP: With FOSHttpCache you can also adapt this to make it cache safe:
13+
https://foshttpcachebundle.readthedocs.io/en/latest/features/helpers/flash-message.html
814
#}
915

10-
<div class="messages">
11-
{% for type, messages in app.flashes %}
12-
{% for message in messages %}
13-
{# Bootstrap alert, see http://getbootstrap.com/components/#alerts #}
14-
<div class="alert alert-dismissible alert-{{ type }} fade in" role="alert">
15-
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
16-
<span aria-hidden="true">&times;</span>
17-
</button>
16+
{% if app.request.hasPreviousSession %}
17+
<div class="messages">
18+
{% for type, messages in app.flashes %}
19+
{% for message in messages %}
20+
{# Bootstrap alert, see http://getbootstrap.com/components/#alerts #}
21+
<div class="alert alert-dismissible alert-{{ type }} fade in" role="alert">
22+
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
23+
<span aria-hidden="true">&times;</span>
24+
</button>
1825

19-
{{ message|trans }}
20-
</div>
26+
{{ message|trans }}
27+
</div>
28+
{% endfor %}
2129
{% endfor %}
22-
{% endfor %}
23-
</div>
30+
</div>
31+
{% endif %}

0 commit comments

Comments
 (0)