正在尝试访问受保护的属性Slim\Http\Request::$attributes,但我收到错误消息
Cannot access protected property Slim\Http\Request::$attributes如何从post请求访问这些内容。这是我的帖子
$this->post('/error/log', function ($request, $response, $arg){
$parsedBody = (object)$request->getParsedBody();
$this->logger->info("Slim-Skeleton '/api/v1/error/log' route");
if ( $this->has('ErrorLogService') ) {
$requestObj = new \stdClass();
$requestObj->data = new \stdClass();
$requestObj->request = $request;
$requestObj->data = $parsedBody;
$resultData = $this->ErrorLogService->saveErrors($requestObj);
}
$http_header = array_key_exists('validation_errors', $resultData ) ? 400 : 200;
$http_header = array_key_exists('error', $resultData ) ? 200 : $http_header;
//var_dump($http_header);die();
$json = unserialize(str_replace(array('NAN;','INF;'),'0;',serialize($resultData)));
return $response->withJson($json, $http_header, JSON_UNESCAPED_SLASHES);
})->add(new Authentication())->setName('error-log');发布于 2020-11-10 15:52:08
尽管您发布的错误消息并不完整,但我猜测问题是在您尝试记录错误时出现的:
$requestObj->request = $request;
...
$resultData = $this->ErrorLogService->saveErrors($requestObj);这里的$request变量表示HTTP请求,是Slim\Http\Request的实例。作为$requestObj的属性,它被传递给日志服务。
会不会是ErrorLogService试图访问受保护的属性?它很可能有一些“魔法”,试图持久化整个对象和它的每个属性。
如果是这种情况,您可以通过以下方法来解决问题:
对于试图访问非公共属性的代码,重新考虑关于HTTP请求的信息应该是logged.
我建议使用第一种方法,因为我知道Slim request对象包含过多的信息,远远超过您想要记录的信息。你可以自己用var_dump($request)检查一下。
https://stackoverflow.com/questions/63825031
复制相似问题