且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何设置“登录"?作为匿名和“注销"的文本对于 twig 和 symfony2 中经过身份验证的用户?

更新时间:2023-12-03 22:31:46

Twig中做如下检查:

{% if app.user %}
    <li><a href="{{ path('logout') }}">{{ 'menu.logout'|trans }}</a></li>
{% else %}
    <li><a href="{{ path('login') }}">{{ 'menu.login'|trans }}</a></li> 
{% endif %}

如果您有不同的角色,并且希望根据用户角色呈现页面的某些部分,您可以进行下一步检查:

In case if you have different roles and you'd like to render some parts of the page dependent on user role, you can do next check:

{% if is_granted('YOUR_ROLE') %} ... {% endif %}