I try to migrate to 3.4 and I have a problem to send a json object.
Up to 3.3, I used the following code:
$jsonSites = json_encode([
'reqLocation' => [
'latitude' => $latitude,
'longitude' => $longitude
],
'sites' => $sitesList,
'discoveryBooks' => $discoveryBooksList,
'deleteSites' => !empty($inDeviceSites) ? [$inDeviceSites] : [],
'deleteBooks' => !empty($inDeviceBooks) ? [$inDeviceBooks] : []
]);
$this->response->type('application/json');
$this->response->body($jsonSites);
And my client received such kind of object:
{
"reqLocation": {
"latitude": 48.080563,
"longitude": 4.4649
},
"sites": [
{
"id": 5076,
"name": "...",
"modified": "2017-01-28T03:03:23+00:00",
"directory_name": "fr/26/26120_56cc30ea4d907",
"type": "portail",
"longitude": 7.031953,
"latitude": 47.939468,
"image_0": "jpg",
"picto_color": "#FFDDDDDD",
"agthemes": [],
"distance": 131.29188575851,
"category": 1281,
"category_name": "Jardin",
"sitecategories": [
{
"id": 10,
"code": 1281,
"name_fr": "Jardin",
"_joinData": {
"id": 1876,
"site_id": 5076,
"site_category_id": 10,
"authorized": true
}
},
{
"id": 33,
"code": 1283,
"name_fr": "Jardin botanique",
"_joinData": {
"id": 5693,
"site_id": 5076,
"site_category_id": 33,
"authorized": true
}
}
]
},
],
"discoveryBooks": [],
"deleteSites": [],
"deleteBooks": []
}
So now I just replaced the deprecated Response methods like that:
EDIT: Of course I return the response object but that cannot work like done here!
$this->response->withType('application/json');
$this->response->withStringBody($jsonSites);
return $this->response;
But now my client doesn't receive anything. I also tried with postman, nothing!
What's wrong?
See Question&Answers more detail:
os