在symfony应用程序中,如何将变量$dynamicPart = 'I always change!';
传递给throw new NotFoundException('error.static.dynamic');
以获取下面的消息?
{
"error": {
"code": "error.static.dynamic",
"message": "Dynamic message is invalid - "I always change!"."
}
}
静态版本工作正常,因为它总是相同的,所以这个throw new NotFoundException('error.static.invalid');
将成功输出:
{
"error": {
"code": "error.static.invalid",
"message": "Static message is invalid."
}
}
当前代码
messages.en.yml
error:
static:
invalid: Static message is invalid.
dynamic:
invalid: Dynamic message is invalid - "%dynamicPart%".
exception.json.twig
{
{
{
'error': {
'code': exception.message,
'message': exception.message|trans
}
}|json_encode|raw
}
}
如果我使用:
throw new NotFoundException(
$this->translator->trans(
'error.static.dynamic',
['%dynamicPart%' => 'I always change!']
)
);
然后我得到:
{
"error": {
"code": "Dynamic message is invalid - "I always change!".",
"message": "Dynamic message is invalid - "I always change!"."
}
}
答案 0 :(得分:0)
在你的树枝上:
{
{
{
'error': {
'code': exception.message,
'message': 'error.dynamic.invalid'|trans({"%dynamicPart%": dynamicPartValue})
}
}|json_encode|raw
}
}