[英]symfony2 unable to receive json response in controller
I am developing REST API with FOSRestBundle. 我正在使用FOSRestBundle开发REST API。 I am unable to send JSON response from controller. 我无法从控制器发送JSON响应。 I am forwarding a request from fooAction of FooControllerFoo to a certain of BarController, when I try to return JSON response from that action I am getting blank array. 我正在将来自FooControllerFoo的fooAction的请求转发到某个BarController,当我尝试从该操作返回JSON响应时,我得到的是空白数组。
here is scenario: 这是场景:
FooController.php 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 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;
}
}
when I print response in FooController's fooAction I see it as an empty array however when I print it in Barcontroller's getByZipCodeAction before returning it I see proper response object with json content. 当我在FooController的fooAction中打印响应时,我将其视为一个空数组,但是当在返回返回之前在Barcontroller的getByZipCodeAction中对其进行打印时,我看到了具有json内容的正确响应对象。
Can anybody help me to figure out what is happening here? 有人可以帮我弄清楚这里发生了什么吗?
EDIT: Response object printed in Barcontroller before returning it: 编辑:返回之前在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] =>
)
================================================================= ================================================== ===============
Response object printed in Foocontroller after getting response from forwarded action: 从转发动作中获得响应后,在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] =>
)
please notice contents are empty. 请注意内容为空。
I'm no expert with FOSRestBundle, but I think you should be doing something like: 我不是FOSRestBundle的专家,但我认为您应该这样做:
$view = $this->view(['one' => 1, 'two' => 2]);
$view->setFormat('json');
return $this->handleView($view);
I believe what's happening at the moment is that the view handler listener receives no data, so overrides the response's content with null. 我相信目前正在发生的事情是视图处理程序侦听器未接收到任何数据,因此使用null覆盖了响应的内容。
I couldn't figure out the exact reason which is causing the issue it could be related to FOSRestBundle view listener. 我无法弄清楚导致该问题的确切原因,该问题可能与FOSRestBundle视图侦听器有关。 Following the link mentioned by @Cerad in his comment, I decided to create service of the action where I was forwarding the request. 通过@Cerad在他的评论中提到的链接,我决定为转发请求的操作创建服务。 So in my case I scrapped the BarController and created a service out of it although I could have opted to use this controller as service however I thought using separate class as a service is more feasible in my case. 因此,就我而言,我报废了BarController并从中创建了一个服务,尽管我可以选择将此控制器用作服务,但是我认为在我的情况下使用单独的类作为服务更为可行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.