我正在嘗試創(chuàng)建自定義標(biāo)準(zhǔn)化器,但無(wú)法使用 API Platform 訪問(wèn)當(dāng)前用戶。當(dāng)我嘗試加載我的 Client 類時(shí),它是空的。我嘗試使用 API 平臺(tái)的文檔進(jìn)行方法,但檢索到的令牌也是空的。您有什么建議可以獲取我當(dāng)前的用戶嗎?謝謝<?phpnamespace App\Serializer;use App\Entity\Stock;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;class StockAttributeNormalizer implements ContextAwareNormalizerInterface{ private $router; private $normalizer; public function __construct(UrlGeneratorInterface $router, ObjectNormalizer $normalizer) { $this->router = $router; $this->normalizer = $normalizer; }public function normalize($topic, $format = null, array $context = []){ $data = $this->normalizer->normalize($topic, $format, $context); var_dump($data); return $data;}public function supportsNormalization($data, $format = null, array $context = []){ return $data instanceof Stock;}}
1 回答

叮當(dāng)貓咪
TA貢獻(xiàn)1776條經(jīng)驗(yàn) 獲得超12個(gè)贊
您是否嘗試過(guò)在您的班級(jí)中注入 TokenInterface ?
public function __construct(UrlGeneratorInterface $router, ObjectNormalizer $normalizer, TokenInterface $token)
? ? {
? ? ? ? $this->router = $router;
? ? ? ? $this->normalizer = $normalizer;
? ? ? ? $this->token = $token;
? ? }
然后您可以用來(lái)$token->getUser()檢索當(dāng)前用戶。
如果您沒(méi)有用戶,那么您就不會(huì)使用帶有身份驗(yàn)證的 API。
- 1 回答
- 0 關(guān)注
- 131 瀏覽
添加回答
舉報(bào)
0/150
提交
取消