如何访问symfony2中未验证页面中的用户角色


How to access the role of user in un-authenticated page in symfony2

在我的主页模板Index.html.twig中,我有一个导航栏,我只想在管理员用户登录时向他们显示,而不是。

我试过这个

{% if is_granted('ROLE_ADMIN') %}
       <div class="navigation">
       </div>
{% endif %}

但是我得到这个错误

在呈现模板的过程中引发了异常("安全上下文不包含身份验证令牌。一个可能的原因可能是没有为此URL配置防火墙。

现在因为这是主页,我不能把它放在防火墙后面。有什么方法可以做到吗

像这样包装一个if块:

{% if app.user is not null %}
    {% if is_granted('ROLE_ADMIN') %}
        {# your code #}
    {% endif %}
{% endif %}