全局访问模板中的用户对象

发布于 2025-01-08 07:42:31 字数 237 浏览 0 评论 0原文

到目前为止,我已经看到了一个 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 技术交流群。

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

发布评论

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

评论(1

落日海湾 2025-01-15 07:42:31

尝试 {{ app.user }}

app 模板变量使您可以访问许多您期望的全局变量,例如 requestsessionsecurity 等。查看源代码对于更多信息。

您应该注意的一件事是,虽然 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, like request, 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() }}

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