全局访问模板中的用户对象
到目前为止,我已经看到了一个 Twig 方法来了解用户权限
{% if is_granted('ROLE_USER') %}
但没有任何关于获取经过身份验证的用户信息的信息,而不在每个控制器操作中传递对象。
在寻找如何将全局变量传递到我的布局之前,我宁愿知道我的布局中是否有任何“标准”方法来访问我的安全提供程序用户对象(至少是实体用户的用户名和数据库信息)或在防火墙覆盖的任何模板中。
I have seen so far a Twig method to know the user privilege
{% if is_granted('ROLE_USER') %}
but nothing about getting authentified user informations, without passing the object in every single controller actions.
Before looking for how to pass a global variable to my layout, I'd rather know if there is any "standard" way to access my security provider user objects (at least the username, and database informations for entity users) in my layout or in any template covered by a firewall.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
{{ app.user }}
。app
模板变量使您可以访问许多您期望的全局变量,例如request
、session
、security
等。查看源代码对于更多信息。您应该注意的一件事是,虽然 GlobalVariables 类为所有变量提供 getter,但 Twig 足够聪明,知道如何处理缩写形式,因此
{{ app .user }}
相当于{{ app.getUser() }}
Try
{{ app.user }}
.The
app
template variable provides you access to a lot of the global variables that you'd expect, likerequest
,session
,security
, etc. Check out the source code for more info.One thing you should be aware of is that while the
GlobalVariables
class provides getters for all of the variables, Twig is smart enough to know how to deal with the short form, so that{{ app.user }}
is equivalent to{{ app.getUser() }}