Symfony 2.4 app.session.flashbag.all()返回空值

时间:2014-04-24 11:17:13

标签: php session symfony twig

使用flashbag信息时遇到问题。我的情况很简单:

我的代码

  1. 使用表单编辑页面:

    # src/Namespace/MyBundle/Resources/views/Edit/form.html.twig
    <form action="{{ path('form_url_save', {'id': id }) }}" method="POST">
        {{ form_widget(form) }}
    </form>
    
  2. 通过控制器将数据表单保存在数据库中:

    # src/Namespace/MyBundle/Controller/EntityController.php
    public function saveAction(Request $request, Entity $entity = null) {
        try {
            if (!$entity) {
                $entity = new Entity();
            }
    
            $form = $this->createForm(new EntityType(), $entity);
    
            if ($request->getMethod() == 'POST') {
                $form->submit($request);
    
                if ($form->isValid()) {
                    // Entity manager
                    $em = $this->getDoctrine()->getManager();
                    // Persist data
                    $em->persist($form->getData());
                    // Saving process
                    $em->flush();
                    // Add flashbag message
                    $this->get('session')->getFlashBag()->add('success', 'The backup was done successfully'));
                } else {
                    throw new \Exception($form->getErrorsAsString());
                }
            }
        } catch (\Exception $e) {
            $this->get('session')->getFlashBag()->add('error', $e->getMessage());
        }
    
        return $this->redirect('home_page_url');
    }
    
  3. 在前面显示成功的消息:

    # app/Resources/views/front.html.twig
    <html>
        <head></head>
        <body>
            <div class="main">
                {% set flashbag = app.session.flashbag.all %}
                {% if flashbag is not empty %}
                    <div class="messages-container">
                        {% for type, messages in flashbag %}
                            {% for message in messages %}
                                <div class="alert alert-{{ type }}">
                                    {{ message }}
                                </div>
                            {% endfor %}
                        {% endfor %}
                    </div>
                {% endif %}
    
                <div class="content">
                    // My content
                </div>
            </div>
        </body>
    </html>
    
  4. 我的主题是如何组织的?

    app/Resources/views/front.html.twig
      |__ src/Namespace/MyBundle/Resources/views/Edit/form.html.twig // extends front.html.twig
    

    我的麻烦:

      {li} app.session.flashbag.all front.html.twig ==&GT; Flashbag是空的 {li} app.session.flashbag.all form.html.twig ==&GT; Flashbag很好并且有成功的消息

    那么为什么我不能把代码放在front.html.twig

2 个答案:

答案 0 :(得分:12)

这是因为FlashBag::all()返回所有消息,然后清空闪存容器。使用FlashBag::peekAll()方法检查flashbag是否包含消息。

示例:

{% if app.session.flashbag.peekAll()|length %}
    {% include 'BraincraftedBootstrapBundle::flash.html.twig' %}
{% endif %}  

答案 1 :(得分:2)

我偶然发现同样的问题并发现了这一点:Symfony2 FlashBag stopped working after upgrade to 2.4?

如果其他主题没有回答您的问题,您可能需要尝试将您的闪光袋丢弃以查看其结构。这样做,尝试在你的树枝模板中添加:

{% set array = app.session.flashbag.all %}
{% dump(array) %}

你可能会对结果感到惊讶,至少我已经:

array(1) { ["test"]=> array(1) { [0]=> string(28) "Ceci est un test de flashbag" } }

这意味着您的flashbag中有消息,但您无法以正确的方式获取内容,因为它位于第二个阵列中。我的解决方案:

{% for tag, line in array %}
  {% for underline in line %}
    <div class="{{tag}}">{{ underline }}</div>
  {% endfor %}
{% endfor %}

希望这有帮助