Symfony2 中的 Flash 消息似乎在我的树枝模板中不起作用

发布于 2024-12-17 12:00:35 字数 533 浏览 1 评论 0原文

我想在我们的页面上添加对 Flash 消息的支持。我实施了 按照此处找到的文档进行操作。

我将以下代码片段添加到我的基本布局中。 (我也尝试添加 它到特定的操作模板)。

{% if app.session.hasFlash('notice') %} 
    <div id="flashmessage" class="flash-notice"> 
       {{ app.session.flash('notice') }} 
   </div> 
{% endif %} 

添加后抛出以下错误

Twig_Error_Runtime:第 66 行的“MyBundle::layout.html.twig”中不存在“”的项目“hasFlash”

我还需要做什么吗?

I want to add support for flash messages on our pages. I implemented
this by following the documentation found here.

I added the following snipplet to my base layout. (i also tried to add
it to a specific action template).

{% if app.session.hasFlash('notice') %} 
    <div id="flashmessage" class="flash-notice"> 
       {{ app.session.flash('notice') }} 
   </div> 
{% endif %} 

After adding the following error is thrown

Twig_Error_Runtime: Item "hasFlash" for "" does not exist in "MyBundle::layout.html.twig" at line 66

Is there anything else i need to do ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

记忆之渊 2024-12-24 12:00:35

你使用 symfony 2.0 还是 2.1 (当前是 master 分支)

对于 symfony 2.1,文档位于此处: http://symfony.com/ doc/2.1/book/controller.html#flash-messages

Flash 消息显示如下:

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

do you use symfony 2.0 or 2.1 (currently master branch)?

for symfony 2.1 the documentation is located here: http://symfony.com/doc/2.1/book/controller.html#flash-messages

flash messages are shown like this:

{% for flashMessage in app.session.flashbag.get('notice') %}
    <div class="flash-notice">
        {{ flashMessage }}
    </div>
{% endfor %}
游魂 2024-12-24 12:00:35

嗯,检查您的配置文件是否已自动启动会话:

session:
    default_locale: %locale%
    auto_start:     true

因为错误似乎是 Twig 找不到会话类,而不是有关 hasFlash 函数的内容。事实上,我的布局中有几乎完全相同的代码。

Mmm check in your config file that you have auto-started the session:

session:
    default_locale: %locale%
    auto_start:     true

Because the error seems to be that Twig doesn't find the session class, not something about the hasFlash function. In fact I have almost exactly the same code in my layout.

百善笑为先 2024-12-24 12:00:35

在撰写本文时,这已经很旧了,所以想象一下您现在已经解决了,但为了参考起见,它是 has 而不是 hasFlash。所以..

{% if app.session.flashbag.has('notice') %} 
    <div id="flashmessage" class="flash-notice"> 
       {{ app.session.flashbag.get('notice') }} 
   </div> 
{% endif %} 

This is pretty old at time of writing so imagine you've worked it out by now, but for reference sake, it's has rather than hasFlash. So..

{% if app.session.flashbag.has('notice') %} 
    <div id="flashmessage" class="flash-notice"> 
       {{ app.session.flashbag.get('notice') }} 
   </div> 
{% endif %} 
旧城空念 2024-12-24 12:00:35

通过 symfony 2.6 +

{% if app.session.flashbag.has('notice') %}
    {{ app.session.flashbag.get('notice').0 }}<br/>
{% endif %}

因为 flashbag 是这个版本的数组,所以你需要 foreach 它或使用索引。我使用索引是因为我不需要更多的东西。

By symfony 2.6 +

{% if app.session.flashbag.has('notice') %}
    {{ app.session.flashbag.get('notice').0 }}<br/>
{% endif %}

Because flashbag is by this version array you need foreach it or use index. I m using index because i dont need something more.

樱花落人离去 2024-12-24 12:00:35

在控制器中

$this->get('session')->getFlashBag()->add('notice', 'Your message!');

在你的 Twig 文件中

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

In controller

$this->get('session')->getFlashBag()->add('notice', 'Your message!');

In your Twig file

{% for flashMessage in app.session.flashbag.get('notice') %}
    <div class="alert alert-warning">{{ flashMessage }}</div>
{% endfor %}  
野心澎湃 2024-12-24 12:00:35

我只是发现,如果在调试模式下 intercept_redirects 为 true,则闪存消息不起作用。

I just figure out that flash messages are not working if intercept_redirects is true in debug mode.

〃温暖了心ぐ 2024-12-24 12:00:35

您是否在操作中的某个位置设置了闪现消息?

$this->get('session')->setFlash('notice', 'Your changes were saved!');

请记住,闪现消息将存储在用户会话中,以满足一个额外请求。

Did you set the flash message somewhere in your action?

$this->get('session')->setFlash('notice', 'Your changes were saved!');

Remember that flash messages will be stored on the user's session for exactly one additional request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文