I'm having trouble with this slim3 php code. On function createErrorReponse function, $response->getBody() is null or empty. Php complains the following error below. As you can see the getBody() size is empty and hence write won't work on it. The same line works in other functions though. HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 0 Fatal error: Call to a member function withHeader() on a non-object in /home/ubuntu/webapp/middleware/authmodule.php on line 16
<?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;
}
}