php - symfony2 difference between error.message and error.messageKey -


i implementing simple custom login form. following 2 different example , official 1 http://symfony.com/doc/current/cookbook/security/form_login_setup.html , other 1 https://knpuniversity.com/screencast/symfony2-ep2/logout#play substantially same differences. giving @ login.html.twig of 2 examples, 1 of differences in error message reporting first reports

<div class="error">{{ error.message|trans }}</div> 

while other reports

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

please here's question : what's difference between "error.message" , "error.messagekey" , error.messagedata means in second example ?

in second example, according doc provided:

"the error variable passed template instance of authenticationexception. may contain more information - or sensitive information - authentication failure, use wisely!"

and class associated:

http://api.symfony.com/2.7/symfony/component/security/core/exception/authenticationexception.html

so variable error sent template , object gotten by:

$error = $authenticationutils->getlastauthenticationerror(); 

in first example, variableerroris class constant gotten :

$error = $session->get(securitycontextinterface::authentication_error); 

and class associated:

http://api.symfony.com/2.0/symfony/component/security/core/securitycontextinterface.html

so can notice both variable error share same name ! not instances of same class

** edit **

this answer commentary, both methods same job

1. first method

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 used translation component.     *     * @return string     */    public function getmessagekey()    {        return 'an authentication exception occurred.';    }     /**     * message data used translation component.     *     * @return array     */    public function getmessagedata()    {        return array();    }  } 

so :

$error = $authenticationutils->getlastauthenticationerror();

followed

{{ error.messagekey|trans(error.messagedata, 'security') }}

will return :

'an authentication exception occurred.'

2. second method

interface securitycontextinterface extends tokenstorageinterface, authorizationcheckerinterface {    const authentication_error = security::authentication_error; }  final class security {     const authentication_error = '_security.last_error'; } 

so

$error = $session->get(securitycontextinterface::authentication_error);

followed by

{{ error.message|trans }}

will return

the last authentication error stored in session


Comments