Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 4:AccessDeniedHttpException()获取访问自定义变量_Symfony_Twig - Fatal编程技术网

Symfony 4:AccessDeniedHttpException()获取访问自定义变量

Symfony 4:AccessDeniedHttpException()获取访问自定义变量,symfony,twig,Symfony,Twig,在my RequestListener.php中,如果用户无法访问实体,我将使用以下异常: throw new AccessDeniedHttpException(); 因此,它使用twig返回我的自定义403错误html模板 <div class="jumbotron jumbotron-fluid exception"> <table class="wrapper"> <tr> <td>

在my RequestListener.php中,如果用户无法访问实体,我将使用以下异常:

throw new AccessDeniedHttpException();
因此,它使用twig返回我的自定义403错误html模板

<div class="jumbotron jumbotron-fluid exception">
    <table class="wrapper">
        <tr>
            <td>
                <div class="error-code">
                    <span>403</span>
                    <div class="caption">
                        <h1 class="text-light">Test h1</h1>
                        <h2 class="text-light">Test h2</h2>
                        <p>
                            Test
                        </p>
                    </div>
                </div>
            </td>
        </tr>
    </table>
</div>
根据发生错误的情况使用不同的消息403

例如,我试图做的是:

throw new AccessDeniedHttpException('custom');
在我的模板403.html.twig中

<div class="jumbotron jumbotron-fluid exception">
<table class="wrapper">
    <tr>
        <td>
            {% if status_text %}
               {{ status_text }}
            {% else %}
            <div class="error-code">
                <span>403</span>
                <div class="caption">
                    <h1 class="text-light">Test h1</h1>
                    <h2 class="text-light">Test h2</h2>
                    <p>
                        Test
                    </p>
                </div>
            </div>
            {% endif %}
        </td>
    </tr>
</table>

{%if状态\文本%}
{{status_text}}
{%else%}
403
测试h1
测试h2

试验

{%endif%}

但是我不知道在我的错误模板中是否可以访问异常中传递的字符串…

请看一下:

您可以在twig中使用
exception.message
变量

<div class="jumbotron jumbotron-fluid exception">
<table class="wrapper">
    <tr>
        <td>
            {% if exception.message %}
               {{ exception.message }}
            {% else %}
            <div class="error-code">
                <span>403</span>
                <div class="caption">
                    <h1 class="text-light">Test h1</h1>
                    <h2 class="text-light">Test h2</h2>
                    <p>
                        Test
                    </p>
                </div>
            </div>
            {% endif %}
        </td>
    </tr>
</table>

{%if exception.message%}
{{exception.message}}
{%else%}
403
测试h1
测试h2

试验

{%endif%}
您可以始终尝试使用
{dump(status\u text)}
查看
状态文本中的内容。对于APP\u ENV=dev,使用APP\u ENV=prod,它会显示“自定义”和正确的403异常(但不是我的html模板,默认情况下是调试页面),dump不工作,并且直接显示变量不显示任何内容…请查看以下内容:。您可以使用twig中的
异常.message
变量实现您想要的。谢谢您的回答:)正是我想要的!
<div class="jumbotron jumbotron-fluid exception">
<table class="wrapper">
    <tr>
        <td>
            {% if exception.message %}
               {{ exception.message }}
            {% else %}
            <div class="error-code">
                <span>403</span>
                <div class="caption">
                    <h1 class="text-light">Test h1</h1>
                    <h2 class="text-light">Test h2</h2>
                    <p>
                        Test
                    </p>
                </div>
            </div>
            {% endif %}
        </td>
    </tr>
</table>