Papan
Papan

Reputation: 382

JsonResponse return string object instead of json in symfony 3.4

I'm trying to return an json object from controller's route

return new JsonResponse($result);

It returns

string(36) "e91a6865-b2a8-4b7d-9351-2eac29f7aa30"
{
   //data
}

How can I get rid of the string(36) "e91a6865-b2a8-4b7d-9351-2eac29f7aa30"?

Upvotes: 0

Views: 751

Answers (1)

Jakumi
Jakumi

Reputation: 8374

a string(36) "e91a6865-b2a8-4b7d-9351-2eac29f7aa30" or something similar, that wasn't added to the JsonResponse, usually is caused by a stray var_dump/dump call somewhere in the code base (but in a part that gets evaluated).

Essentially what happens: the var_dump/dump is called and it produces the output, and afterwards the output from the Response object (be it the JsonResponse or some other Response) is appended to that.

Options to handle this:

  • remember where you put those calls ;o) (works most of the time)
  • do not commit any var_dump/dump/dd calls to version control ever (and always do version control!), you'll find the calls easily in the git diff.
  • run grep -nr dump src in the project root (on linux obviously, replace dump by dd if appropriate), this should find the relevant locations in code.

However, overall it's a rather benign source of irritating output.

Upvotes: 1

Related Questions