本文介绍了api平台请求问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个实体,作者和书
I have two entities author and book
book | manytoOne |author
id | |id
idAuthor |
在author控制器中,我有一个Action函数,该函数可以获取作者的详细信息及其所有书籍(使用forward函数获取书籍)
in the author controller i have an Action function that gets the author details and all his books (using the forward function to get the books)
public function authorDetailsAction($id){
$author= $this->getDoctrine()->getRepository(Author::class)->find($id);
$books= $this->forward('AppBundle:Book:authorBooks',array('id' => $id));
return array('author' => $author, 'books' => $books);
}
或者当我只对书籍进行退回$ books 时,出现此错误
or when im rutrning only the books return $books
i got this error
提前感谢您的指导
推荐答案
在使用API平台时,无需创建自定义控制器就可以实现所需的功能:
As it looks like you use API Platform, you can achieve what you want without creating a custom controller:
- 向
Author
资源添加过滤器,以便能够按名称搜索作者: https://api-platform.com/docs/core/serialization-groups-and-relations#embedding-relations - 嵌入与作者直接相关的作者的书: https://api-platform.com/docs/core/filters#search-filter .
- Add a filter to the
Author
resource to be able to search an author by name: https://api-platform.com/docs/core/serialization-groups-and-relations#embedding-relations - Embed books related to an author directly in the author response: https://api-platform.com/docs/core/filters#search-filter.
这篇关于api平台请求问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!