我正在使用Symfony 3.4中的FOSUserBundle。一切正常。我用自己的模板覆盖FOSUserBundle模板。我检查用户是否由于密码或电子邮件不正确而导致登录失败的方法是:
login.html.twig
{% trans_default_domain 'FOSUserBundle' %}
...Some html nothing fancy
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
....Rest of file content
</div>
这没有问题。在通过我的安全文件处理失败后,用户当然会重定向到索引页面。
但是,我尝试在index.html.twig中使用上面的if语句,它认为它是未定义的。我想使用它,例如,当用户重定向到索引页面以显示我的自定义消息时,我可以使用它。设置完该树枝文件后,该树枝文件中的“错误”变量是否变为全局变量?
答案 0 :(得分:0)
FosUserBundle将错误存储在symfony flashBag中,您可以使用此代码
const results = [];
for (const group of groups)
results.push(await this.checkVotes(group));
您可以看到此模板是FosUserBundle https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/layout.html.twig
或更全球化https://symfony.com/doc/current/controller.html#flash-messages
如果要更改默认错误消息,则应替换FosUserBundle的默认翻译。