为了账号安全,请及时绑定邮箱和手机立即绑定

我如何将客户端数据访问到自定义规范器(Symfony 4)

我如何将客户端数据访问到自定义规范器(Symfony 4)

PHP
慕哥6287543 2023-07-01 15:17:23
我正在尝试创建自定义标准化器,但无法使用 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条经验 获得超12个赞

您是否尝试过在您的班级中注入 TokenInterface ?


public function __construct(UrlGeneratorInterface $router, ObjectNormalizer $normalizer, TokenInterface $token)

    {

        $this->router = $router;

        $this->normalizer = $normalizer;

        $this->token = $token;

    }

然后您可以用来$token->getUser()检索当前用户。


如果您没有用户,那么您就不会使用带有身份验证的 API。


查看完整回答
反对 回复 2023-07-01
  • 1 回答
  • 0 关注
  • 72 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信