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

Php symfony2无法在控制器中接收json响应,php,json,symfony,fosrestbundle,Php,Json,Symfony,Fosrestbundle,我正在用FOSRestBundle开发restapi。我无法从控制器发送JSON响应。我正在将一个请求从FooControllerFoo的fooAction转发到某个BarController,当我尝试从该操作返回JSON响应时,我得到的是空白数组 下面是一个场景: FooController.php class FooController extends FOSRestController { ... public function fooAction() { ... $re

我正在用FOSRestBundle开发restapi。我无法从控制器发送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

...

}

}
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;

     }

    }
BarController.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

...

}

}
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并用它创建了一个服务,虽然我可以选择将此控制器用作服务,但我认为在我的例子中使用单独的类作为服务更可行。

您是否尝试使用
JsonResponse
对象而不是
Response
?@YannEugonéyes尝试了JsonResponse获得相同的空白响应是由于子请求,因为我使用了forward?这里有一些讨论:。是的,我确实认为FOSRestBundle视图侦听器是罪魁祸首。@Rahul
$this->forward()
应该返回
响应。您知道它正在转发到正确的控制器操作吗?我刚刚编辑了问题标题,因为我能够发送json响应。但是,当我收到内容为空时,我将尝试使用view obj发送json响应,并将共享结果No change与response相同的空数组