Skip to content

Commit 2362ac8

Browse files
committed
Fix session starting issue in symfony-demo on flash messages
Use of flash messages triggers session start, which as of last year triggers Symfony to mark response as private. Fix the issue by checking if there is a session first as adviced on: https://symfony.com/doc/current/session/avoid_session_start.html
1 parent 6169279 commit 2362ac8

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)