我正在嘗試創(chuàng)建自定義標準化器,但無法使用 API Platform 訪問當前用戶。當我嘗試加載我的 Client 類時,它是空的。我嘗試使用 API 平臺的文檔進行方法,但檢索到的令牌也是空的。您有什么建議可以獲取我當前的用戶嗎?謝謝<?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 回答

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