8000 fixed example to check if the session exists when getting flash messages to avoid starting sessions when not needed by fabpot · Pull Request #2564 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

fixed example to check if the session exists when getting flash messages t 8000 o avoid starting sessions when not needed #2564

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
May 5, 2013
Merged
Show file tree
Hide file tree
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
24 changes: 14 additions & 10 deletions book/controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -686,19 +686,23 @@ the ``notice`` message:

.. code-block:: html+jinja

{% for flashMessage in app.session.flashbag.get('notice') %}
<div class="flash-notice">
{{ flashMessage }}
</div>
{% endfor %}
{% if app.session.started %}
{% for flashMessage in app.session.flashbag.get('notice') %}
<div class="flash-notice">
{{ flashMessage }}
</div>
{% endfor %}
{% endif %}

.. code-block:: html+php

<?php foreach ($view['session']->getFlashBag()->get('notice') as $message): ?>
<div class="flash-notice">
<?php echo "<div class='flash-error'>$message</div>" ?>
</div>
<?php endforeach; ?>
<?php if ($view['session']->isStarted()): ?>
<?php foreach ($view['session']->getFlashBag()->get('notice') as $message): ?>
<div class="flash-notice">
<?php echo "<div class='flash-error'>$message</div>" ?>
</div>
<?php endforeach; ?>
<?php endif; ?>

By design, flash messages are meant to live for exactly one request (they're
"gone in a flash"). They're designed to be used across redirects exactly as
Expand Down
13 changes: 13 additions & 0 deletions components/http_foundation/sessions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,16 @@ Compact method to process display all flashes at once::
echo "<div class='flash-$type'>$message</div>\n";
}
}

.. caution::

As flash messages use a session to store the messages from one request to
the next one, a session will be automatically started when you read the
flash messages even if none already exists. To avoid that default
behavior, test if there is an existing session first::

if ($session->isStarted()) {
foreach ($session->getFlashBag()->get('warning', array()) as $message) {
echo "<div class='flash-warning'>$message</div>";
}
}
8 changes: 5 additions & 3 deletions quick_tour/the_controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,11 @@ next request::

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

{% for flashMessage in app.session.flashbag.get('notice') %}
<div>{{ flashMessage }}</div>
{% endfor %}
{% if app.session.started %}
{% for flashMessage in app.session.flashbag.get('notice') %}
<div>{{ flashMessage }}</div>
{% endfor %}
{% endif %}

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