Php Symfony2:覆盖表单登录中的异常消息

Php Symfony2:覆盖表单登录中的异常消息,php,symfony,twig,Php,Symfony,Twig,我已构建了由此控制器操作呈现的登录表单: public function loginAction() { $helper = $this->get('security.authentication_utils'); return $this->render('SecurityLoginBundle:Login:login.html.twig', array( 'last_username' => $helper->getLa

我已构建了由此控制器操作呈现的登录表单:

public function loginAction() {
    $helper = $this->get('security.authentication_utils');
    return $this->render('SecurityLoginBundle:Login:login.html.twig', array(
                'last_username' => $helper->getLastUsername(),
                'error' => $helper->getLastAuthenticationError(),
    ));
}
如果用户未提供有效的电子邮件/密码,“getLastAuthenticationError()”将抛出“BadCredentialsException”;如果用户被禁用,则抛出“DisabledException”。两个异常对象都有“message”属性,但我想更改标签。我该怎么做

一个丑陋的解决方法是在我的小树枝模板中阅读这些信息,然后用我的措辞替换它们,但有更好的方法吗?比如检查Twig中“error”参数的类。不幸的是

get_class($helper->getLastAuthenticationError())
未工作-它返回“Security\LoginBundle\Controller\LoginController”

谢谢

我也有同样的问题。 我一看到就解决了

class Symfony\Component\Security\Http\AuthenticationAuthenticationUtils 
getLastAuthenticationError具有默认参数$clearSession=true

因此,您可以在清除会话后处理错误

    $helper = $this->get('security.authentication_utils');
    $error = $helper->getLastAuthenticationError();
如果错误是某个异常的实例,也可以在自定义消息中引发异常

    if ($error instanceof BadCredentialsException) {
         throw \Exception('Your Custom exception'); 
    }
如果需要,可以添加try-catch,并使用自定义消息创建新的var$error。 在返回内容消息后,因为它是一个例外:

    return $this->render('SecurityLoginBundle:Login:login.html.twig', array(
            'last_username' => $helper->getLastUsername(),
            'error' => $error ? $error->getMessage() : null,
    )); 
我也有同样的问题。 我一看到就解决了

class Symfony\Component\Security\Http\AuthenticationAuthenticationUtils 
getLastAuthenticationError具有默认参数$clearSession=true

因此,您可以在清除会话后处理错误

    $helper = $this->get('security.authentication_utils');
    $error = $helper->getLastAuthenticationError();
如果错误是某个异常的实例,也可以在自定义消息中引发异常

    if ($error instanceof BadCredentialsException) {
         throw \Exception('Your Custom exception'); 
    }
如果需要,可以添加try-catch,并使用自定义消息创建新的var$error。 在返回内容消息后,因为它是一个例外:

    return $this->render('SecurityLoginBundle:Login:login.html.twig', array(
            'last_username' => $helper->getLastUsername(),
            'error' => $error ? $error->getMessage() : null,
    )); 
检查这个答案检查这个答案