首页 > 解决方案 > Symfony 4 addFlash 出现在网络选项卡上,但不在页面上

问题描述

我在这里遇到的奇怪的事情。我已经从 S4 Doc 复制了所有内容,但它似乎没有按预期工作。控制器代码:

// Adding a success type message
$this->addFlash("success", "This is a success message");
// Adding a warning type message
$this->addFlash("warning", "This is a warning message");
// Adding an error type message
$this->addFlash("error", "This is an error message");
// Adding a custom type message, remember the type is totally up to you !
$this->addFlash("bat-alarm", "Gotham needs Batman");
// 2. Retrieve manually the flashbag
// Retrieve flashbag from the controller
$flashbag = $this->get('session')->getFlashBag();
// Set a flash message
$flashbag->add("other", "This is another flash message with other type");
// Render some twig view
$carList = $this->getDoctrine()->getRepository(CarProject::class)->findAll();                
return $this->render('index.html.twig', array ('carList' => $carList));

index.html.twig 代码:

<div>
      {% for flash_message in app.session.flashBag.get('success') %}
        <div class="alert alert-success">
        {{ flash_message }}
   </div>
      {% endfor %}
   </div>

我可以看到它已加载到 Chrome DevTools Network 选项卡上,是列表中的最新响应,但它并未出现在实际页面上。 在此处输入图像描述 有任何想法吗?

标签: phpsymfonycontrollertwig

解决方案


我的评论给出了明确的答案,以便您可以解决这个问题:您正在尝试进行 AJAX 闪烁,这在纯树枝中是不可能的。

您应该查找诸如toastr 之类的东西或使用一些 jquery 插件。


推荐阅读