Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix session starting issue on flash messages usage #859

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

Merged
merged 1 commit into from
Oct 4, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions templates/default/_flash_messages.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@
A common practice to better distinguish between templates and fragments is to
prefix fragments with an underscore. That's why this template is called
'_flash_messages.html.twig' instead of 'flash_messages.html.twig'

We check if we have session before reading flashes as it otherwise triggers session start:
https://symfony.com/doc/current/session/avoid_session_start.html

TIP: With FOSHttpCache you can also adapt this to make it cache safe:
https://foshttpcachebundle.readthedocs.io/en/latest/features/helpers/flash-message.html
#}

<div class="messages">
{% for type, messages in app.flashes %}
{% for message in messages %}
{# Bootstrap alert, see http://getbootstrap.com/components/#alerts #}
<div class="alert alert-dismissible alert-{{ type }} fade in" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
{% if app.request.hasPreviousSession %}
<div class="messages">
{% for type, messages in app.flashes %}
{% for message in messages %}
{# Bootstrap alert, see http://getbootstrap.com/components/#alerts #}
<div class="alert alert-dismissible alert-{{ type }} fade in" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>

{{ message|trans }}
</div>
{{ message|trans }}
</div>
{% endfor %}
{% endfor %}
{% endfor %}
</div>
</div>
{% endif %}