在Twig异常控制器中未定义Symfony2用户 [英] Symfony2 User not defined in Twig Exception Controller

查看:118
本文介绍了在Twig异常控制器中未定义Symfony2用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自己的操作来处理树枝中的异常.

I use my own action in order to handle exception in twig.

# Twig Configuration
twig:
    exception_controller:  MyBundle:Default:showException

问题是,我实际上已对我的应用程序进行了身份验证,但是在此特殊Controller中,令牌为null.

The problem is that, i'm actually authenticated to my application, but in this special Controller the token is null.

有人有解决此问题的想法,因为我在树枝模板中使用了用户对象.

Does someone have any idea to solve this problem, cause I use the user object in the twig template.

推荐答案

来自设计自定义错误页面时的常见陷阱之一是在错误模板(或错误模板继承的任何父模板)中使用is_granted()函数.如果这样做,您将看到Symfony引发的异常.

One of the common pitfalls when designing custom error pages is to use the is_granted() function in the error template (or in any parent template inherited by the error template). If you do that, you'll see an exception thrown by Symfony.

此问题的原因是路由是在安全性之前完成的.如果发生404错误,则不会加载安全层,因此is_granted()函数是未定义的.解决方案是在使用此功能之前添加以下检查:

The cause of this problem is that routing is done before security. If a 404 error occurs, the security layer isn't loaded and thus, the is_granted() function is undefined. The solution is to add the following check before using this function:

{% if app.user and is_granted('...') %}
    {# ... #}
{% endif %}

这篇关于在Twig异常控制器中未定义Symfony2用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆