【问题标题】:How to receive a message, instead of throwing exception in Symfony?如何接收消息,而不是在 Symfony 中抛出异常?
【发布时间】:2017-04-29 09:34:02
【问题描述】:

我有一个在电子商务网站上显示用户包的功能。当用户包为空时,会抛出如下异常:

if (!$bag) {
    throw  new NotFoundHttpException();
}

我怎样才能显示诸如“你的包是空的”之类的消息,而不是扔一个

404 未找到 - NotFoundHttpException

提前致谢!

【问题讨论】:

    标签: php symfony exception


    【解决方案1】:

    有很多方法可以做到。

    您可以使用flash messages 甚至render twig template 来通知空包。

    或者您可以将表明袋子为空的参数传递给模板,然后您可以在模板中使用该参数。

    或者你可以在树枝模板中检查$bag对象,比如

    {% if bag is null %}Your bag is empty{% endif %}

    【讨论】:

    • Flash 消息正是为此目的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 2015-10-07
    • 2017-04-01
    • 2013-07-12
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    相关资源
    最近更新 更多