I started learning PHP Slim-Framework v3. But I'm finding it difficult on few occasions.
Here is my code:
$app = new \Slim\App(["settings" => $config]);
$app->get('/', function(Request $request, Response $response, $args = []) {
$error = array('result' => false, 'message' => 'Bad Request', 'dev'=>'', 'data' => []);
$response->withStatus(500)->getBody()->write(json_encode($error));
});
Now I want to respond with status 500 to the user when ever I have issues in service. But unfortunately this is not working. Though I'm getting a response, it is returning 200 status instead of 500.
Am I doing something wrong or am I missing something?
I tried looking into other issues but I did not find anything helping me out.
The
Response
-object is immutable, therefore it cannot be changed. The methodswith*()
do return a copy of theResponse
-object with the changed value.See this answer why you dont need to reassign the value on
write
.You can also use
withJson
instead: