Symfony2 中的 Flash 消息似乎在我的树枝模板中不起作用
我想在我们的页面上添加对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你使用 symfony 2.0 还是 2.1
(当前是 master 分支)?对于 symfony 2.1,文档位于此处: http://symfony.com/ doc/2.1/book/controller.html#flash-messages
Flash 消息显示如下:
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:
嗯,检查您的配置文件是否已自动启动会话:
因为错误似乎是 Twig 找不到会话类,而不是有关 hasFlash 函数的内容。事实上,我的布局中有几乎完全相同的代码。
Mmm check in your config file that you have auto-started the session:
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.
在撰写本文时,这已经很旧了,所以想象一下您现在已经解决了,但为了参考起见,它是
has
而不是hasFlash
。所以..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 thanhasFlash
. So..通过 symfony 2.6 +
因为 flashbag 是这个版本的数组,所以你需要 foreach 它或使用索引。我使用索引是因为我不需要更多的东西。
By symfony 2.6 +
Because flashbag is by this version array you need foreach it or use index. I m using index because i dont need something more.
在控制器中
在你的 Twig 文件中
In controller
In your Twig file
我只是发现,如果在调试模式下
intercept_redirects
为 true,则闪存消息不起作用。I just figure out that flash messages are not working if
intercept_redirects
is true in debug mode.您是否在操作中的某个位置设置了闪现消息?
请记住,闪现消息将存储在用户会话中,以满足一个额外请求。
Did you set the flash message somewhere in your action?
Remember that flash messages will be stored on the user's session for exactly one additional request.