Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fixed example to check if the session exists when getting flash messages to 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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
weaverryan merged 1 commit intosymfony:2.1fromfabpot:session
May 5, 2013
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsbook/controller.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionscomponents/http_foundation/sessions.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsquick_tour/the_controller.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp