Symfony 3.4 中的 Flash 消息



我正在尝试从联系人操作设置闪存消息,然后在主页上重定向,但是在上面,我看不到我的闪存消息,也许我的会话已重置?我可以得到一些帮助吗,我是Symfony的初学者。

包含索引和联系函数的核心控制器:

<?php
namespace OCCoreBundleController;
use SymfonyBundleFrameworkBundleControllerController;
class CoreController extends Controller
{
public function indexAction()
{
$ServiceAdverts = $this->container->get('oc_core.listAdverts');
$adList = $ServiceAdverts->getListAdverts();
return $this->render("OCCoreBundle:Core:index.html.twig", array(
'listAdverts' => $adList
));
}
public function contactAction()
{
$this->addFlash('info', 'Contact page not ready yet !');
return $this->redirectToRoute('oc_core_homepage');
}
}

树枝模板(主页(:

{% block body %}
<div>
Messages flash :
{% for msg in app.session.flashBag.get('info') %}
<div class="alert alert-success">
{{ msg }}
</div>
{% endfor %}
</div>
<h2>Liste des annonces</h2>
<ul>
{% for advert in listAdverts %}
<li>
<a href="{{ path('oc_platform_view', {'id': advert.id}) }}">
{{ advert.title }}
</a>
par {{ advert.author }},
le {{ advert.date|date('d/m/Y') }}
</li>
{% else %}
<li>Pas (encore !) d'annonces</li>
{% endfor %}
</ul>
<a href="{{ path('oc_core_contact') }}">Contact</a>
{% endblock %}

Symfony 3.3对flash消息进行了改进,因此您的Twig模板应该看起来不同。app.session.flashBag.get()调用现在替换为app.flashes()

所以你的Twig代码现在将是:

{% for msg in app.flashes('success') %}
<div class="alert alert-success">
{{ msg }}
</div>
{% endfor %}

试试这个,在 3.2 和 3.4 中对我有用

{% for type, flash_messages in app.session.flashBag.all %}
{% for msg in flash_messages %}
<div class="alert alert-{{ type }}">
{{ msg }}
</div>
{% endfor %}
{% endfor %}

另一件事是,一旦你调用flashBag,它就会变成空的,所以你不能使用它两次。检查您的代码是否在第二次重定向之前没有在另一个页面上调用它......

最新更新