我是 Symfony 的新手。我使用 LexicJWT 令牌。
final class ProductCreateSubscriber implements EventSubscriberInterface
{
private $entityManager;
private $hostRepository;
public function __construct(EntityManagerInterface $entityManager, HostRepository $hostRepository)
{
$this->entityManager = $entityManager;
$this->hostRepository = $hostRepository;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['createHost', EventPriorities::PRE_WRITE],
];
}
public function createHost(GetResponseForControllerResultEvent $event)
{
$product = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
- - - - - -- - - I NEED USER HERE - - - - - - -
if (!$product instanceof Product || Request::METHOD_POST !== $method) {
return;
}
$parsedUrl = parse_url($product->getUrl());
if (isset($parsedUrl['host'])) {
$host = $this->hostRepository->findOneByName($parsedUrl['host']);
if (!$host) {
$host = $this->hostRepository->createByName($parsedUrl['host']);
}
$product->setHost($host);
}
}
}
如何在 createHost 方法中获取用户?如何在 EventSubsriber 中获取用户或令牌?找不到这方面的信息?我在哪里可以读到它?