symfony2无法在控制器中接收json响应


symfony2 unable to receive json response in controller

我正在使用FOSRestBundle开发REST API。我无法从控制器发送JSON响应。我正在将FooControllerFoo的fooAction的请求转发到BarController的某个操作,当我试图从该操作返回JSON响应时,我得到的是空白数组。

这是一个场景:

FooController.php

class FooController  extends FOSRestController {
...
public function fooAction() {
 ...
    $response = $this->forward('ApplicationPApiBundle:bar:getByZipCode', array(), array('zipcode' => $zipcode));
    print_r($response);  //getting blank array here
...
}
}

BarController.php

class BarController  extends FOSRestController {
    ...
    public function getByZipCodeAction() {
     ...
       $response = new Response((array("one"=>"1", "two"=> "2")));
       $response->headers->set('Content-Type', 'application/json');
       // print_r($response);  // after printing I can see data in json format
       return $response;
     }
    }

当我在FooController的fooAction中打印响应时,我将其视为一个空数组,但当我在返回它之前在Barcontroller的getByZipCodeAction中打印它时,我会看到具有json内容的正确响应对象。

有人能帮我弄清楚这里发生了什么吗?

编辑:返回之前在Barcontroller中打印的响应对象:

Symfony'Component'HttpFoundation'Response Object
(
    [headers] => Symfony'Component'HttpFoundation'ResponseHeaderBag Object
        (
            [computedCacheControl:protected] => Array
                (
                    [no-cache] => 1
                )
            [cookies:protected] => Array
                (
                )
            [headerNames:protected] => Array
                (
                    [cache-control] => Cache-Control
                    [date] => Date
                    [content-type] => Content-Type
                )
            [headers:protected] => Array
                (
                    [cache-control] => Array
                        (
                            [0] => no-cache
                        )
                    [date] => Array
                        (
                            [0] => Wed, 24 Sep 2014 13:48:49 GMT
                        )
                    [content-type] => Array
                        (
                            [0] => application/json
                        )
                )
            [cacheControl:protected] => Array
                (
                )
        )
    [content:protected] => {"one":"1","two":"2"}
    [version:protected] => 1.0
    [statusCode:protected] => 200
    [statusText:protected] => OK
    [charset:protected] => 
)

=============================================

从转发操作获得响应后,Foocontroller中打印的响应对象:

Symfony'Component'HttpFoundation'Response Object
(
    [headers] => Symfony'Component'HttpFoundation'ResponseHeaderBag Object
        (
            [computedCacheControl:protected] => Array
                (
                    [no-cache] => 1
                )
            [cookies:protected] => Array
                (
                )
            [headerNames:protected] => Array
                (
                    [cache-control] => Cache-Control
                    [date] => Date
                    [content-type] => Content-Type
                    [x-debug-token] => X-Debug-Token
                    [x-debug-token-link] => X-Debug-Token-Link
                )
            [headers:protected] => Array
                (
                    [cache-control] => Array
                        (
                            [0] => no-cache
                        )
                    [date] => Array
                        (
                            [0] => Wed, 24 Sep 2014 13:48:49 GMT
                        )
                    [content-type] => Array
                        (
                            [0] => application/json
                        )
                    [x-debug-token] => Array
                        (
                            [0] => 168be3
                        )
                    [x-debug-token-link] => Array
                        (
                            [0] => /app_dev.php/_profiler/168be3
                        )
                )
            [cacheControl:protected] => Array
                (
                )
        )
    [content:protected] => []
    [version:protected] => 1.0
    [statusCode:protected] => 200
    [statusText:protected] => OK
    [charset:protected] => 
)

请注意内容为空。

我不是FOSRestBundle的专家,但我认为你应该做一些类似的事情:

$view = $this->view(['one' => 1, 'two' => 2]);
$view->setFormat('json');
return $this->handleView($view);

我相信目前发生的情况是,视图处理程序侦听器没有接收到任何数据,因此用null覆盖响应的内容。

我无法找出导致问题的确切原因——它可能与FOSRestBundle视图侦听器有关。根据@Cerad在评论中提到的链接,我决定在转发请求的地方创建行动服务。因此,在我的案例中,我废弃了BarController,并用它创建了一个服务,尽管我本可以选择将此控制器用作服务,但我认为在我的情况下,使用单独的类作为服务更可行。