繁体   English   中英

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并从中创建了一个服务,尽管我可以选择将此控制器用作服务,但是我认为在我的情况下使用单独的类作为服务更为可行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM