升级Api-Platform 2.7后getCollection接口报资源类未找到异常
问题详情
当前API基于PHP8.1、Symfony 6.1和Api-Platform 2.6,计划升级至Api-Platform 3.0。先升级到Api-Platform 2.7并将metadata_backward_compatibility_layer标志设为false后,所有getCollection端点出现错误,其他端点运行正常。抛出异常ApiPlatform\Exception\ResourceClassNotFoundException,提示信息:Resource "App\Entity\RatingAttribute" not found。
相关实体代码
<?php declare(strict_types=1); namespace App\Entity; use ApiPlatform\Metadata\Post; use ApiPlatform\Metadata\GetCollection; use ApiPlatform\Metadata\Patch; use ApiPlatform\Metadata\Get; use ApiPlatform\Metadata\ApiResource; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table("rating_attribute")] #[ApiResource( operations: [new Get(), new Patch(), new GetCollection(), new Post()], paginationEnabled: false )] class RatingAttribute extends AbstractReadWriteProperties { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: 'integer', nullable: true)] private ?int $id = null; #[ORM\ManyToOne(targetEntity: Rating::class)] private Rating $rating; #[ORM\Column(type: 'string', length: 240)] private string $key; #[ORM\Column(type: 'string', length: 240)] private string $value; #[ORM\Column(type: 'bigint', length: 240)] private string $reviewToken; /** * @return int|null */ public function getId(): ?int { return $this->id; } /** * @return Rating */ public function getRating(): Rating { return $this->rating; } /** * @return string */ public function getKey(): string { return $this->key; } /** * @return string */ public function getValue(): string { return $this->value; } /** * @return string */ public function getReviewToken(): string { return $this->reviewToken; } /** * @param Rating $rating * * @return RatingAttribute */ public function setRating(Rating $rating): RatingAttribute { $this->rating = $rating; return $this; } /** * @param string $key * * @return RatingAttribute */ public function setKey(string $key): RatingAttribute { $this->key = $key; return $this; } /** * @param string $value * * @return RatingAttribute */ public function setValue(string $value): RatingAttribute { $this->value = $value; return $this; } /** * @param string $reviewToken * * @return RatingAttribute */ public function setReviewToken(string $reviewToken): RatingAttribute { $this->reviewToken = $reviewToken; return $this; } }
问题定位
错误来自vendor/api-platform/core/src/Core/Metadata/Resource/Factory/ExtractorResourceMetadataFactory.php第54行的这段代码:
if (!(class_exists($resourceClass) || interface_exists($resourceClass)) || !$resource = $this->extractor->getResources()[$resourceClass] ?? false) { return $this->handleNotFound($parentResourceMetadata, $resourceClass); }
注释掉这段代码后,所有功能均可正常运行。
内容的提问来源于stack exchange,提问作者Simon
相关产品推荐
相关产品推荐

