无法从容器获取IriConverter,因为它不是公共的

问题描述 投票:1回答:1

我尝试加载api_platform.iri_converter,但收到错误:

\“ api_platform.iri_converter \”服务或别名在编译容器时已被删除或内联。您应该将其公开,或者停止直接使用容器并改为使用依赖项注入。

这是代码:

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);
        }
    }
}
php symfony symfony4 api-platform.com symfony-dependency-injection
1个回答
1
投票

不要注入容器。

相反,直接注入IriConverter

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

abstract class BaseVoter extends Voter
{
    public IriConverterInterface $iriConverter;

    public function __construct(IriConverterInterface $iriConverter)
    {
        $this->iriConverter = $iriConverter;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.