簡體   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