File tree 3 files changed +32
-13
lines changed
components/http_foundation
3 files changed +32
-13
lines changed Original file line number Diff line number Diff line change @@ -686,19 +686,23 @@ the ``notice`` message:
686
686
687
687
.. code-block :: html+jinja
688
688
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 %}
694
696
695
697
.. code-block :: html+php
696
698
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; ?>
702
706
703
707
By design, flash messages are meant to live for exactly one request (they're
704
708
"gone in a flash"). They're designed to be used across redirects exactly as
Original file line number Diff line number Diff line change @@ -333,3 +333,16 @@ Compact method to process display all flashes at once::
333
333
echo "<div class='flash-$type'>$message</div>\n";
334
334
}
335
335
}
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
+ }
Original file line number Diff line number Diff line change @@ -141,9 +141,11 @@ next request::
141
141
142
142
// display any messages back in the next request (in a template)
143
143
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 %}
147
149
148
150
This is useful when you need to set a success message before redirecting
149
151
the user to another page (which will then show the message). Please note that
You can’t perform that action at this time.
0 commit comments