无法从容器中获取 IriConverter,因为它不是 public

Can't get the IriConverter from the container because it's not public

我试图加载 api_platform.iri_converter 但出现错误:

The \"api_platform.iri_converter\" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.

这是代码:

declare(strict_types=1);

namespace App\Security\Authorization\Voter;

use Symfony\Component\DependencyInjection\ContainerInterface;

abstract class BaseVoter extends Voter
{
    public ContainerInterface $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }
}
declare(strict_types=1);

namespace App\Security\Authorization\Voter;

class VenueVoter extends BaseVoter
{
    protected function voteOnAttribute(): bool
    {
        /** @var User $tokenUser */
        $tokenUser = $token->getUser();

        if (self::VENUE_CREATE === $attribute) {
            $iri = $this->container->get('api_platform.iri_converter')->getItemFromIri($valueWithIri);
        }
    }
}

不要注入容器。

而是直接注入 IriConverter

use ApiPlatform\Core\Bridge\Symfony\Routing\IriConverterInterface;

abstract class BaseVoter extends Voter
{
    public IriConverterInterface $iriConverter;

    public function __construct(IriConverterInterface $iriConverter)
    {
        $this->iriConverter = $iriConverter;
    }
}