Skip to content

Commit 1501120

Browse files
committed
Merge pull request symfony#2564 from fabpot/session
fixed example to check if the session exists when getting flash messages to avoid starting sessions when not needed
2 parents 63e4a8d + a5168ad commit 1501120

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

book/controller.rst

+14-10
Original file line numberDiff line numberDiff line change
@@ -686,19 +686,23 @@ the ``notice`` message:
686686

687687
.. code-block:: html+jinja
688688

689-
{% for flashMessage in app.session.flashbag.get('notice') %}
690-
<div class="flash-notice">
691-
{{ flashMessage }}
692-
</div>
693-
{% endfor %}
689+
{% if app.session.started %}
690+
{% for flashMessage in app.session.flashbag.get('notice') %}
691+
<div class="flash-notice">
692+
{{ flashMessage }}
693+
</div>
694+
{% endfor %}
695+
{% endif %}
694696

695697
.. code-block:: html+php
696698

697-
<?php foreach ($view['session']->getFlashBag()->get('notice') as $message): ?>
698-
<div class="flash-notice">
699-
<?php echo "<div class='flash-error'>$message</div>" ?>
700-
</div>
701-
<?php endforeach; ?>
699+
<?php if ($view['session']->isStarted()): ?>
700+
<?php foreach ($view['session']->getFlashBag()->get('notice') as $message): ?>
701+
<div class="flash-notice">
702+
<?php echo "<div class='flash-error'>$message</div>" ?>
703+
</div>
704+
<?php endforeach; ?>
705+
<?php endif; ?>
702706

703707
By design, flash messages are meant to live for exactly one request (they're
704708
"gone in a flash"). They're designed to be used across redirects exactly as

components/http_foundation/sessions.rst

+13
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,16 @@ Compact method to process display all flashes at once::
333333
echo "<div class='flash-$type'>$message</div>\n";
334334
}
335335
}
336+
337+
.. caution::
338+
339+
As flash messages use a session to store the messages from one request to
340+
the next one, a session will be automatically started when you read the
341+
flash messages even if none already exists. To avoid that default
342+
behavior, test if there is an existing session first::
343+
344+
if ($session->isStarted()) {
345+
foreach ($session->getFlashBag()->get('warning', array()) as $message) {
346+
echo "<div class='flash-warning'>$message</div>";
347+
}
348+
}

quick_tour/the_controller.rst

+5-3
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,11 @@ next request::
141141

142142
// display any messages back in the next request (in a template)
143143

144-
{% for flashMessage in app.session.flashbag.get('notice') %}
145-
<div>{{ flashMessage }}</div>
146-
{% endfor %}
144+
{% if app.session.started %}
145+
{% for flashMessage in app.session.flashbag.get('notice') %}
146+
<div>{{ flashMessage }}</div>
147+
{% endfor %}
148+
{% endif %}
147149

148150
This is useful when you need to set a success message before redirecting
149151
the user to another page (which will then show the message). Please note that

0 commit comments

Comments
 (0)