Laravel JSON响应返回受保护的数据

时间:2022-10-18 20:36:06
return Response::json(array('status' => 'Group not found'));

returns protected data. Here's the JSON:

返回受保护的数据这是JSON:

{"status":"Group not found"}

{“status”:“未找到群组”}

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 }

object(Illuminate \ Http \ JsonResponse)#320(10){[“jsonOptions”:protected] => int(0)[“data”:protected] => string(28)“{”status“:”未找到组“}”[“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) “星期二,2014年6月17日19:03:33 GMT”}} [“cacheControl”:protected] => array(0){}} [“content”:protected] => string(28)“{”status“: “未找到组”}“[”版本“:protected] => string(3)”1.0“[”statusCode“:protected] => int(200)[”s tatusText“: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)?

看看[“data”:protected] => string(28)“{”status“:”未找到组“}”。由于某种原因,数据受到保护,并且在解码JSON时不会出现。我如何“取消保护”它(公开发布)?

1 个解决方案

#1


6  

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:

祖先Response类具有:

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.

您的数据存储在成员受保护的$ content中是可以的,因为当JsonResponse对象强制转换为字符串时,PHP使用__toString()方法的返回值作为该对象的字符串表示形式。

#1


6  

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:

祖先Response类具有:

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.

您的数据存储在成员受保护的$ content中是可以的,因为当JsonResponse对象强制转换为字符串时,PHP使用__toString()方法的返回值作为该对象的字符串表示形式。