api平台请求问题 [英] Api-platform request issue

查看:104
本文介绍了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);

}

但仅显示作者信息

but it shows only author informations

或者当我只对书籍进行退回$ books 时,出现此错误

or when im rutrning only the books return $books i got this error

控制器必须返回响应(Array(0 => Object(AppBundle \ Entity \ Book),1 => Object(AppBundle \ Entity \ Book),2 => Object(AppBundle \ Entity \ Book)).(500个内部服务器错误)

The controller must return a response (Array(0 => Object(AppBundle\Entity\Book), 1 => Object(AppBundle\Entity\Book), 2 => Object(AppBundle\Entity\Book)) given). (500 Internal Server Error)

提前感谢您的指导

推荐答案

在使用API​​平台时,无需创建自定义控制器就可以实现所需的功能:

As it looks like you use API Platform, you can achieve what you want without creating a custom controller:

  1. Author 资源添加过滤器,以便能够按名称搜索作者: https://api-platform.com/docs/core/filters#search-filter .
  1. 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
  2. Embed books related to an author directly in the author response: https://api-platform.com/docs/core/filters#search-filter.

这篇关于api平台请求问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆