如何解决使用KernelEvents的Symfony EventSubscriber向我抛出Offset 0错误
因此,我正在学习创建API的基础。 我正在尝试使用API在创建新用户时进行交互,但是我需要使用symfony来哈希密码。
我制作了一个PasswordEncoderSubscriber方法,该方法在将密码插入数据库之前会对密码进行哈希处理。
private $encoder;
public function __construct(PasswordEncoderInterface $encoder)
{
$this->encoder = $encoder;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['encodePassword' => EventPriorities::PRE_WRITE]
];
}
public function encodePassword(ViewEvent $event)
{
$result = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if ($result instanceof User && $method === "POST") {
$hash = $this->encoder->encodePassword($result,$result->getPassword());
$result->setPassword($hash);
}
}
在使用KernelEvents::View
将函数写入数据库之前,我正在使用encodePassword
来调用函数EventPriorities::PRE_WRITE
。
这是我得到的错误:注意:未定义的偏移量:0 。
在KernelEvents::VIEW
我忘记了某些内容之后,代码就中断了吗?
谢谢!
解决方法
根据symfony manual,您应该提供处理程序,并将其作为具有2个项目的数组作为优先级:
return [
KernelEvents::EXCEPTION => [
['processException',10],],];
因此,您的代码应为:
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => [
['encodePassword',EventPriorities::PRE_WRITE],]
];
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。