使用 Api-Platform 时,为什么会出现“没有与类型关联的项目路径”?

Mar*_*mal 6 php symfony api-platform.com

我的项目是在 Symfony 5 和 API-Platform 中构建的,以便从 JS 客户端使用它。在这种情况下,我有一个Question与实体相关的Answer实体(分别是一对多)。

每当我尝试打电话/api/questions/api/answers收到此错误时:“没有与类型“App\Entity\Answer”相关联的项目路由

为了 API 资源,这是我使用 maker bundle 配置的内容:

- App\Entity\Question

/**
 * @ORM\Entity(repositoryClass=QuestionRepository::class)
 * @ApiResource
 * @ApiFilter(SearchFilter::class, properties={"status": "exact"})
 */
class Question
{
    // ...
    /**
     * @ORM\ManyToOne(targetEntity=User::class, inversedBy="questions")
     * @ORM\JoinColumn(nullable=false)
     */
    private $owner;

    // ... 

    /**
     * @ORM\OneToMany(targetEntity=Answer::class, mappedBy="question", orphanRemoval=true)
     */
    private $answers;

    // ...

    /**
     * @return Collection|Answer[]
     */
    public function getAnswers(): Collection
    {
        return $this->answers;
    }

    public function addAnswer(Answer $answer): self
    {
        if (!$this->answers->contains($answer)) {
            $this->answers[] = $answer;
            $answer->setQuestion($this);
        }

        return $this;
    }

    public function removeAnswer(Answer $answer): self
    {
        if ($this->answers->contains($answer)) {
            $this->answers->removeElement($answer);
            // set the owning side to null (unless already changed)
            if ($answer->getQuestion() === $this) {
                $answer->setQuestion(null);
            }
        }

        return $this;
    }

    // ...
}
Run Code Online (Sandbox Code Playgroud)

- App\Entity\Answer

/**
 * @ORM\Entity(repositoryClass=AnswerRepository::class)
 * @ApiResource
 */
class Answer
{
    // ...
    
    /**
     * @ORM\ManyToOne(targetEntity=Question::class, inversedBy="answers")
     * @ORM\JoinColumn(nullable=false)
     * @Assert\NotBlank
     */
    private $question;

    // ...

    public function getQuestion(): ?Question
    {
        return $this->question;
    }

    public function setQuestion(?Question $question): self
    {
        $this->question = $question;

        return $this;
    }
   
    // ...
}
Run Code Online (Sandbox Code Playgroud)

不确定这是否与此问题相关:我配置config/api_platform/resources.yamlapi_platform.yaml

App\Entity\Answer:
  attributes:
    security: 'is_granted("ROLE_USER")'
    security_message: 'Only the authenticated user can see answers'
  collectionOperations:
    get:
      security: 'is_granted("ROLE_USER")'
      security_message: 'Only the authenticated user can get answers'
    post:
      security: 'is_granted("ROLE_USER")'
      security_message: 'Only the authenticated user can save answers'
  itemOperations:
      put:
        security: 'is_granted("ROLE_ADMIN") or object.owner == user'
        security_message: 'Only the authenticated user or administrators can update answers'
      delete:
        security: 'is_granted("ROLE_ADMIN")'
        security_message: 'Only administrators can delete answers'

App\Entity\Question:
  attributes:
    security: 'is_granted("ROLE_USER")'
    security_message: 'Only the authenticated user can see questions'
  collectionOperations:
    get:
      security: 'is_granted("ROLE_ADMIN")'
      security_message: 'Only the authenticated user or administrators can get questions'
    post:
      security: 'is_granted("ROLE_USER")'
      security_message: 'Only the authenticated user can save questions'
  itemOperations:
      get:
        security: 'is_granted("ROLE_ADMIN") or object.owner == user'
        security_message: 'Only the authenticated user or administrators can get questions'
      put:
        security: 'is_granted("ROLE_ADMIN") or object.owner == user'
        security_message: 'Only the authenticated user or administrators can update questions'
      delete:
        security: 'is_granted("ROLE_ADMIN")'
        security_message: 'Only administrators can delete questions'
Run Code Online (Sandbox Code Playgroud)

有关扬鞭UI显示的信息QuestionAnswer实体资源:

这是debug:route关于Answer以下内容的显示:

  api_answers_get_collection                      GET        ANY      ANY    /api/answers.{_format}
  api_answers_post_collection                     POST       ANY      ANY    /api/answers.{_format}
  api_answers_put_item                            PUT        ANY      ANY    /api/answers/{id}.{_format}
  api_answers_delete_item                         DELETE     ANY      ANY    /api/answers/{id}.{_format}
Run Code Online (Sandbox Code Playgroud)

这里有什么问题?

yiv*_*ivi 10

域名注册地址:

对于所有资源,您需要拥有基本GET /resource/{id}路线。



错误信息非常明确:

没有与“App\Entity\Answer”类型关联的项目路由

如果您检查您的资源定义,Answer您将看到:

itemOperations:
      put:
        security: 'is_granted("ROLE_ADMIN") or object.owner == user'
        security_message: 'Only the authenticated user or administrators can update answers'
      delete:
        security: 'is_granted("ROLE_ADMIN")'
        security_message: 'Only administrators can delete answers'
Run Code Online (Sandbox Code Playgroud)

所以你基本上只有删除更新路线,但没有基本的项目检索路线(GET /api/answers/123)。通过明确定义路由,您未定义的任何路由都将禁用

API 文档的输出证实了这一点: 在此处输入图片说明

没有它,就不可能获得资源的有效 IRI。如果没有GET资源的有效路由,系统就无法为每个资源项生成 URL,并且由于这些 URL/IRI 被系统用作标识符,因此应始终定义它们。

您只需要为 item 路由添加配置:

itemOperations:
      put:
        security: 'is_granted("ROLE_ADMIN") or object.owner == user'
        security_message: 'Only the authenticated user or administrators can update answers'
      delete:
        security: 'is_granted("ROLE_ADMIN")'
        security_message: 'Only administrators can delete answers'
      get:
        security: 'is_granted("ROLE_USER")'
        security_message: 'Only the authenticated user can get answers'
Run Code Online (Sandbox Code Playgroud)