gpt4 book ai didi

symfony - FOSRestBundle: Controller 必须返回给定的响应(Array())

转载 作者:行者123 更新时间:2023-12-02 16:30:42 24 4
gpt4 key购买 nike

我从 FOSRestBundle 开始。我添加了这个路由配置:

//Sermovi/Bundle/APIBundle/Resources/config/routing.yml    
sermovi_api_homepage:
type: rest
resource: Sermovi\Bundle\APIBundle\Controller\DefaultController

//app/config/routing.yml
sermovi_api:
type: rest
prefix: /api
resource: "@SermoviAPIBundle/Resources/config/routing.yml"

还有这个 config.yml

fos_rest:
routing_loader:
default_format: json
view:
view_response_listener: true

sensio_framework_extra:
view:
annotations: false

这个 Controller :

namespace Sermovi\Bundle\APIBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Response;


class DefaultController extends FOSRestController
{
public function getArticlesAction()
{
$em = $this->getDoctrine()->getManager();

$entity = $em->getRepository('SermoviManagementBundle:Transaction')->find(776);

return array(
'entity' => $entity
);
}
}

我收到此错误:

[{"message":"The controller must return a response (Array(entity => Object(Sermovi\Bundle\ManagementBundle\Entity\Transaction)) given).","class":"LogicException","trace":[{"namespace":"","short_class":"","class":"","type":"","function":"","file":"/home/tirengarfio/workspace/sermovi/app/bootstrap.php.cache","line":2855,"args":[]},{"namespace":"Symfony\Component\HttpKernel","short_class":"HttpKernel","class":"Symfony\Component\HttpKernel\HttpKernel","type":"->","function":"handleRaw","file":"/home/tirengarfio/workspace/sermovi/app/bootstrap.php.cache","line":2817,"args":[["object","Symfony\Component\HttpFoundation\Request"],["string","1"]]},{"namespace":"Symfony\Component\HttpKernel","short_class":"HttpKernel","class":"Symfony\Component\HttpKernel\HttpKernel","type":"->","function":"handle","file":"/home/tirengarfio/workspace/sermovi/app/bootstrap.php.cache","line":2946,"args":[["object","Symfony\Component\HttpFoundation\Request"],["string","1"],["boolean",true]]},{"namespace":"Symfony\Component\HttpKernel\DependencyInjection","short_class":"ContainerAwareHttpKernel","class":"Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel","type":"->","function":"handle","file":"/home/tirengarfio/workspace/sermovi/app/bootstrap.php.cache","line":2247,"args":[["object","Symfony\Component\HttpFoundation\Request"],["string","1"],["boolean",true]]},{"namespace":"Symfony\Component\HttpKernel","short_class":"Kernel","class":"Symfony\Component\HttpKernel\Kernel","type":"->","function":"handle","file":"/home/tirengarfio/workspace/sermovi/web/app_dev.php","line":28,"args":[["object","Symfony\Component\HttpFoundation\Request"]]}]}]

编辑:

“我可以”做下面这样的事情吗?或者由于 FOSRestBundle 使用 JMSSerializerBundle 我不应该这样做?

$serializedEntity = $this->container->get('serializer')->serialize($entity, 'json');
return new Response($serializedEntity);

最佳答案

有多种方法可以使用 FOSRestBundle 设置 Controller 。按照你这样做的方式,你必须返回一个 View 。这是 github 上的休息 Controller 的链接,可能会对您有所帮助。 LiipHelloBundle有一个例子。

此外,我发现使用ClassResourceInterface最简单。在我的 Controller 中。这样,我返回一个数组,它本身处理所有序列化。它还使用您的 Controller 名称来生成必要的路由,因此我不必手动定义任何路由。这是我设置 Controller 的首选方式。请参阅文档条目 here了解它是如何工作的。

如果您最终使用 ClassResourceInterface ,请确保为每个操作包含以下注释,它将使您返回的数组正确序列化:

use FOS\RestBundle\Controller\Annotations as Rest;
//.....

/**
* @Rest\View()
*/
public function cgetAction() {}

您甚至可以通过设置 Controller 的方式来做到这一点,但我以前没有尝试过。如果您这样做并且有效,请告诉我们。

更新

对于那些可能有兴趣使用 FOSRestBundle 而不使用 ClassResourceInterface 的人,问题中 Controller 操作的问题是它不返回 View 。这应该在操作中起作用:

class DefaultController extends FOSRestController
{
public function getArticlesAction()
{
$em = $this->getDoctrine()->getManager();

$entity = $em->getRepository('SermoviManagementBundle:Transaction')->find(776);
$statusCode = 200;

$view = $this->view($entity, $statusCode);
return $this->handleView($view);
}
}

关于symfony - FOSRestBundle: Controller 必须返回给定的响应(Array()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20688910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com