I am developing REST API with FOSRestBundle. I am unable to send JSON response from controller. 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.
here is scenario:
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;
}
}
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.
Can anybody help me to figure out what is happening here?
EDIT: Response object printed in Barcontroller before returning it:
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:
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:
$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.
I couldn't figure out the exact reason which is causing the issue it could be related to FOSRestBundle view listener. Following the link mentioned by @Cerad in his comment, I decided to create service of the action where I was forwarding the request. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.