0

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.

Rahul
  • 2,189
  • 7
  • 40
  • 60
  • Did you try to use `JsonResponse` object instead of `Response` ? – Yann Eugoné Sep 24 '14 at 14:47
  • @YannEugoné yes tried JsonResponse getting same blank response – Rahul Sep 24 '14 at 14:52
  • Is it due to subrequest since I am using forward? – Rahul Sep 24 '14 at 14:57
  • There is some discussion here: https://github.com/FriendsOfSymfony/FOSRestBundle/issues/310 . And yes, I do think the FOSRestBundle view listener is the culprit. – Cerad Sep 24 '14 at 15:04
  • @Rahul `$this->forward()` should return a `Response`. Do you **know** that it's forwarding to the correct controller action? – Dan Blows Sep 24 '14 at 15:05
  • yes I am sure its forwarding to correct controller action, I am able to print response object before returning it to calling controller – Rahul Sep 24 '14 at 15:09

2 Answers2

0

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.

Dan Blows
  • 20,846
  • 10
  • 65
  • 96
  • I just edited question title since i am able to send json response however when i receive it content are blank, i will try to send json response with view obj, will share the outcome – Rahul Sep 24 '14 at 15:32
  • No change same empty array as response – Rahul Sep 24 '14 at 15:56
0

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.

Rahul
  • 2,189
  • 7
  • 40
  • 60