5
votes
return Response::json(array('status' => 'Group not found'));

returns protected data. Here's the JSON:

{"status":"Group not found"}

The following code

//$jsonData - the data returned above

var_dump($jsonData);

returns this:

object(Illuminate\Http\JsonResponse)#320 (10) { ["jsonOptions":protected]=> int(0) ["data":protected]=> string(28) "{"status":"Group not found"}" ["callback":protected]=> NULL ["encodingOptions":protected]=> int(15) ["headers"]=> object(Symfony\Component\HttpFoundation\ResponseHeaderBag)#317 (5) { ["computedCacheControl":protected]=> array(1) { ["no-cache"]=> bool(true) } ["cookies":protected]=> array(0) { } ["headerNames":protected]=> array(3) { ["cache-control"]=> string(13) "Cache-Control" ["content-type"]=> string(12) "Content-Type" ["date"]=> string(4) "Date" } ["headers":protected]=> array(3) { ["cache-control"]=> array(1) { [0]=> string(8) "no-cache" } ["content-type"]=> array(1) { [0]=> string(16) "application/json" } ["date"]=> array(1) { [0]=> string(29) "Tue, 17 Jun 2014 19:03:33 GMT" } } ["cacheControl":protected]=> array(0) { } } ["content":protected]=> string(28) "{"status":"Group not found"}" ["version":protected]=> string(3) "1.0" ["statusCode":protected]=> int(200) ["statusText":protected]=> string(2) "OK" ["charset":protected]=> NULL }

Take a look at ["data":protected]=> string(28) "{"status":"Group not found"}". The data is protected for some reason and doesn't appear when I decode the JSON. How do I "unprotect" it (make it publicly available)?

1
I'm having the exact same issue with the Laravel application as you are having. Just as a co-worker of mine said, frameworks make the diffuclt things easy and the easy things difficultThe One and Only ChemistryBlob
Facing same issue, have you got any solution on this?Vinod Jadhav

1 Answers

6
votes

I don't think this is your issue.

If you look at the inheritance tree:

\Symfony\Component\HttpFoundation\Response
    \Symfony\Component\HttpFoundation\JsonResponse
        \Illuminate\Http\JsonResponse

The ancestor Response class has:

public function __toString()
{
    ...
    return ... . $this->getContent();
}

So we follow:

public function getContent()
{
    return $this->content;
}

It's okay that your data is stored inside member protected $content because when the JsonResponse object is cast to a string, PHP uses the return value of the __toString() method to be the string representation of that object.