Laravel JSON 响应返回受保护的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24271509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel JSON response returns protected data
提问by Onion
return Response::json(array('status' => 'Group not found'));
returns protected data. Here's the JSON:
返回受保护的数据。这是 JSON:
{"status":"Group not found"}
{"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 }
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) "日期" } ["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 June 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 }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 }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 }string(3) "1.0" ["statusCode":protected]=> int(200) ["statusText":protected]=> string(2) "OK" ["charset":protected]=> NULL }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)?
看看["data":protected]=> string(28) "{"status":"Group not found"}"
。由于某种原因,数据受到保护,并且在我解码 JSON 时不会出现。我如何“取消保护”它(使其公开可用)?
回答by Carbon
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.
您的数据存储在成员内部是可以的,protected $content
因为当 JsonResponse 对象被转换为字符串时,PHP 使用该__toString()
方法的返回值作为该对象的字符串表示。