所以我正在學習創建 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我忘記了什么嗎?之后代碼就中斷了?
1 回答

四季花海
TA貢獻1811條經驗 獲得超5個贊
根據symfony 手冊,您應該提供處理程序,并且它的優先級為包含 2 個項目的數組:
return [
? ? KernelEvents::EXCEPTION => [
? ? ? ? ['processException', 10],
? ? ],
];
所以,你的代碼應該是:
public static function getSubscribedEvents()
{
? ? return [
? ? ? ? KernelEvents::VIEW => [
? ? ? ? ? ? ['encodePassword', EventPriorities::PRE_WRITE],
? ? ? ? ]
? ? ];
}
- 1 回答
- 0 關注
- 93 瀏覽
添加回答
舉報
0/150
提交
取消