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

时间:2014-06-17 19:05:31

标签: json laravel

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

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

{"status":"Group not found"}

以下代码

//$jsonData - the data returned above

var_dump($jsonData);

返回:

  

object(Illuminate \ Http \ JsonResponse)#320(10){   [ “jsonOptions”:保护] => int(0)[“data”:protected] =>串(28)   “{”status“:”未找到群组“}”[“callback”:protected] =>空值   [ “encodingOptions”:保护] => int(15)[“headers”] =>   object(Symfony \ Component \ HttpFoundation \ ResponseHeaderBag)#317(5){   [ “computedCacheControl”:保护] => array(1){[“no-cache”] =>   bool(true)} [“cookies”:protected] =>数组(0){}   [ “headerNames”:保护] => array(3){[“cache-control”] =>串(13)   “Cache-Control”[“content-type”] => string(12)“Content-Type”   [ “日期”] => string(4)“Date”} [“headers”:protected] => array(3){   [ “缓存控制”] => array(1){[0] => string(8)“no-cache”}   [ “内容类型”] => array(1){[0] => string(16)“application / json”}   [ “日期”] => array(1){[0] => string(29)“星期二,2014年6月17日19:03:33 GMT”   } [“cacheControl”:protected] => array(0){}}   [ “内容”:保护] => string(28)“{”status“:”找不到组“}”   [ “版本”:保护] => string(3)“1.0”[“statusCode”:protected] =>   int(200)[“statusText”:protected] => string(2)“OK”   [ “字符集”:保护] => NULL}

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

1 个答案:

答案 0 :(得分:6)

我认为这不是你的问题。

如果你看一下继承树:

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

祖先Response类有:

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

所以我们遵循:

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

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

相关问题