登录失败时如何在 symfony2 中写入特定的错误消息取决于具体情况
例如: 未找到用户名时: 用户名“userxxx”不存在。 找到用户名但密码错误时: 您输入的密码错误!请再试一次!或者: 'userxxx' 的密码错误!请再试一次!
我找到了一种在找不到用户名时警告错误消息的方法是编写自定义用户提供程序
class UserProvider implements UserProviderInterface {
protected $entityManager;
public function __construct(EntityManager $entityManager) {
$this->entityManager = $entityManager;
}
public function loadUserByUsername($username) {
$user = $this->entityManager
->getRepository('ShopSecurityBundle:User')
->findOneBy(array('username' => $username));
if ($user) {
$service_user = new SecurityServiceUser($user->getUsername(), $user->getPassword(), $user->getSalt(), array($user->getRole()));
//var_dump($service_user); die;
return $service_user;
}
// I throw a BadCredentialsException() here then the error message in security Context will be changed arcodding to the exception message!
throw new BadCredentialsException(sprintf('Username "%s" does not exist', $username));
}
// ...
}
但是我不知道当密码错误而用户名正确时如何更改错误消息!
有什么建议吗?