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);
        }
    }
}
4

1 回答 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;
    }
}
于 2020-03-05T18:42:42.793 回答