代码之家  ›  专栏  ›  技术社区  ›  Thibault Dumas

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

  •  0
  • Thibault Dumas  · 技术社区  · 7 年前

    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>
    

    现在我想做的是,同样的:

    抛出新的AccessDeniedHttpException();
    

    根据发生错误的情况使用不同的消息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>
    

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

    1 回复  |  直到 7 年前
        1
  •  2
  •   Marc-André    7 年前

    也许你可以看看这个: How to display exception text in a custom error page on Symfony?

    你可以使用 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>