我正在实现一个简单的自定义登录表单。我正在关注两个不同的例子,官方的http://symfony.com/doc/current/cookbook/security/form_login_setup.html和另一个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的含义是什么?
答案 0 :(得分:3)
在第二个示例中,根据您提供的文档:
&#34;传递给模板的错误变量是AuthenticationException的一个实例。它可能包含有关身份验证失败的更多信息 - 甚至是敏感信息,因此请明智地使用它!&#34;
与班级相关:
http://api.symfony.com/2.7/Symfony/Component/Security/Core/Exception/AuthenticationException.html
因此发送给模板的变量error
是和对象获取的:
$error = $authenticationUtils->getLastAuthenticationError();
在第一个例子中,变量error
是类常量得到的:
$error = $session->get(SecurityContextInterface::AUTHENTICATION_ERROR);
与班级相关:
http://api.symfony.com/2.0/Symfony/Component/Security/Core/SecurityContextInterface.html
所以你可以注意到变量error
只共享同一个名字!它们不是同一类的实例
** 编辑 **
这是对你的评论的回答,两种方法都做同样的工作
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-&gt; getLastAuthenticationError();
其次是
{{error.messageKey | trans(error.messageData,&#39; security&#39;)}}
将返回:
&#39;发生了身份验证例外。&#39;
interface SecurityContextInterface extends TokenStorageInterface, AuthorizationCheckerInterface
{
const AUTHENTICATION_ERROR = Security::AUTHENTICATION_ERROR;
}
final class Security
{
const AUTHENTICATION_ERROR = '_security.last_error';
}
所以
$ error = $ session-&gt; get(SecurityContextInterface :: AUTHENTICATION_ERROR);
其次是
{{error.message | trans}}
将返回
存储在会话
中的最后一次身份验证错误
答案 1 :(得分:1)
在第一种情况下(java.lang.SecurityException: The Maps API requires the additional following permissions to be set in the AndroidManifest.xml to ensure a correct behavior:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
at com.google.maps.api.android.lib6.d.t.a()
at com.google.maps.api.android.lib6.d.ft.a()
at com.google.maps.api.android.lib6.d.aj.a()
at com.google.maps.api.android.lib6.d.ai.a()
at com.google.android.gms.maps.internal.x.onTransact(SourceFile:107)
at android.os.Binder.transact(Binder.java:361)
at com.google.android.gms.maps.internal.IMapFragmentDelegate$zza$zza.onCreateView()
at com.google.android.gms.maps.SupportMapFragment$zza.onCreateView()
at com.google.android.gms.dynamic.zza$4.zzb()
at com.google.android.gms.dynamic.zza.zza()
at com.google.android.gms.dynamic.zza.onCreateView()
at com.google.android.gms.maps.SupportMapFragment.onCreateView()
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
at com.eTilbudsavis.eTilbudsavis.fragment.LocationMapFragment.ensureSupportMapFragment(LocationMapFragment.java:338)
at com.eTilbudsavis.eTilbudsavis.fragment.LocationMapFragment.onStart(LocationMapFragment.java:676)
at android.support.v4.app.Fragment.performStart(Fragment.java:1813)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:989)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(NativeStart.java)
)error.message|trans
只保留翻译密钥。
第二个(error.message
)稍微复杂一点:
您在error.messageKey|trans(error.messageData, 'security')
您的数据将用于填充已翻译字符串中的占位符(某种error.messageKey
请参阅http://symfony.com/doc/current/book/translation.html#translations-in-templates
您有邮件域sprintf('test: %s', messageData)
(将邮件整理成组的可选方式),请参阅http://symfony.com/doc/current/components/translation/introduction.html#using-message-domains