我遇到了这个 slim3 php 代码的问题。在函数 createErrorReponse 函数中,$response->getBody() 为 null 或空。PHP 在下面抱怨以下错误。如您所见, getBody() 大小为空,因此 write 无法对其进行操作。不过,同一行也适用于其他功能。HTTP/1.1 200 OK 内容类型:text/html;charset=UTF-8 0 致命错误:在第 16 行的 /home/ubuntu/webapp/middleware/authmodule.php 中的非对象上调用成员函数 withHeader()
<?php
class AuthenticationMiddleware
{
public function isAuthenticated($userid, $authorization)
{
//do data validation here
return false;
}
public function createErrorResponse($code, $msg, $response)
{
echo $response;
echo $response->getBody()->getSize();
$response = $response->getBody()->write(json_encode('holla'));
$response = $response->withHeader('Content-Type', 'application/json; charset=utf-8');
return $response;
}
public function __invoke($request, $response, $next)
{
$userid = $request->getHeaderLine('userid');
$authorization = $request->getHeaderLine('Authorization');
if($this->isAuthenticated($userid, $authorization))
{
$response = $next($request, $response);
}
else
{
$msg = 'You are unauthenticated. Please login again';
$code = 400;
$response = $this->createErrorResponse($code, $msg, $response);
}
return $response;
}
}