error.message 和 error.messageKey 之间的 symfony2 区别

symfony2 difference between error.message and error.messageKey

我正在实现一个简单的自定义登录表单。我正在关注两个不同的示例,官方示例 http://symfony.com/doc/current/cookbook/security/form_login_setup.html and this other one https://knpuniversity.com/screencast/symfony2-ep2/logout#play 基本相同但有一些差异。 看一下这两个示例的 login.html.twig,其中一个区别在于第一个报告

的错误消息报告
<div class="error">{{ error.message|trans }}</div>

而其他报告

div class="error">{{ error.messageKey|trans(error.messageData, 'security') }}</div>

请问我的问题:"error.message" 和 "error.messageKey" 之间有什么区别,error.messageData 在第二个例子中是什么意思?

在第二个示例中,根据您提供的文档:

"The error variable passed into the template is an instance of AuthenticationException. It may contain more information - or even sensitive information - about the authentication failure, so use it wisely!"

与class关联:

http://api.symfony.com/2.7/Symfony/Component/Security/Core/Exception/AuthenticationException.html

所以发送到模板的变量 error 是对象:

$error = $authenticationUtils->getLastAuthenticationError();

在第一个例子中,变量error是class常数:

$error = $session->get(SecurityContextInterface::AUTHENTICATION_ERROR);

与class关联:

http://api.symfony.com/2.0/Symfony/Component/Security/Core/SecurityContextInterface.html

因此您可以注意到两个变量 error 仅共享相同的名称!它们不是相同 class

的实例

** 编辑 **

这是对您的评论的回答,两种方法的作用相同

1。第一种方法

class AuthenticationUtils
{
    /**
     * @param bool $clearSession
     *
     * @return AuthenticationException|null
     */
    public function getLastAuthenticationError($clearSession = true)
    {
        $request = $this->getRequest();
        $session = $request->getSession();
        $authenticationException = null;

        if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
            $authenticationException = $request->attributes->get(Security::AUTHENTICATION_ERROR);
        } elseif ($session !== null &&   $session->has(Security::AUTHENTICATION_ERROR)) {
            $authenticationException = $session->get(Security::AUTHENTICATION_ERROR);

            if ($clearSession) {
                $session->remove(Security::AUTHENTICATION_ERROR);
            }
        }

        return $authenticationException;
    }



class AuthenticationException extends \RuntimeException implements \Serializable
  {


   /**
    * Message key to be used by the translation component.
    *
    * @return string
    */
   public function getMessageKey()
   {
       return 'An authentication exception occurred.';
   }

   /**
    * Message data to be used by the translation component.
    *
    * @return array
    */
   public function getMessageData()
   {
       return array();
   }
 }

所以 :

$error = $authenticationUtils->getLastAuthenticationError();

其次是

{{ error.messageKey|trans(error.messageData, 'security') }}

会 return :

'An authentication exception occurred.'

2。第二种方法

interface SecurityContextInterface extends TokenStorageInterface, AuthorizationCheckerInterface
{
   const AUTHENTICATION_ERROR = Security::AUTHENTICATION_ERROR;
}

final class Security
{
    const AUTHENTICATION_ERROR = '_security.last_error';
}

所以

$error = $session->get(SecurityContextInterface::AUTHENTICATION_ERROR);

其次是

{{ error.message|trans }}

会return

the last authentication error stored in session

在第一种情况下 (error.message|trans) error.message 只持有翻译密钥。

第二个(error.messageKey|trans(error.messageData, 'security'))稍微复杂一点: