Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel JSON response returns protected data

Tags:

json

laravel

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)?

like image 980
Onion Avatar asked Jun 17 '14 19:06

Onion


1 Answers

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.

like image 56
Carbon Avatar answered Oct 22 '22 15:10

Carbon