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.