Symfony4-如何将TokenStorageInterface传递给EntityListener?

时间:2018-06-28 09:13:50

标签: dependency-injection symfony4

我目前正在使用api平台创建并使用Symfony4的项目。

我的目标是能够在EntityListener中使用TokenStorage来了解我的用户的身份。

不幸的是,我找不到将TokenStorageInterface作为参数传递的方法。 而且自动接线系统似乎无法正常工作。

services.yaml:

# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
    locale: 'en'

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        public: false       # Allows optimizing the container by removing unused services; this also means
                            # fetching services directly from the container via $container->get() won't work.
                            # The best practice is to be explicit about your dependencies anyway.


    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/*'
        exclude: '../src/{Entity,Migrations,Tests}'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones
    App\EntityListener\KpiDateListener:
        public: true,
        arguments:
            $tokenStorage: '@security.token_storage'

KpiDateListener.php:

<?php

namespace App\EntityListener;

use ApiPlatform\Core\Tests\Fixtures\TestBundle\Manager\UserManager;
use App\Entity\KpiDate;
use App\Entity\StateDate;
use App\Security\User\UserProvider;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Psr\Log\LoggerInterface;
use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class KpiDateListener
{
    private $tokenStorage;

    public function __construct(TokenStorageInterface $tokenStorage)
    {
        $this->tokenStorage = $tokenStorage;
    }

    public function postPersist(KpiDate $kpiDate, LifecycleEventArgs $args)
    {
        //Here is my function
    }
}

我总是有相同的错误:

  

函数App \ EntityListener \ KpiDateListener :: __ construct()的参数太少,在第76行的/srv/api/vendor/doctrine/doctrine-bundle/Mapping/ContainerAwareEntityListenerResolver.php中传递了0,并且恰好是期望的1

有什么问题或遗漏吗?

谢谢

1 个答案:

答案 0 :(得分:0)

该错误消息将表明未为您的侦听器启用自动装配。自动装配适用于服务。如果您的实体侦听器未注册为服务,则DI组件将无法对其进行自动接线,因为它不知道它(然后,实例化类将由Doctrine本身执行)。

尝试更改为:

    App\EntityListener\KpiDateListener:
        autowire: true
        public: true,
        arguments:
            $tokenStorage: '@security.token_storage'
        tags:
            - { name: doctrine.orm.entity_listener }