Hey i’ve been trying slim for several days now i am able to make simple API like so
$app->get('/articles/getcategories', function (Request $request, Response $response, array $args) {
(new Category) -> getCategories($response);
});
one problem is when i want to send a custom response code to my end point from my class, in this case Category class it doesn’t work, i was able to send a response using this method :
$response -> getBody() -> write(json_encode($this -> makeObject(704, 'Something went wrong!')));
But when i look at the documentation :
$newResponse = $response->withStatus(302);
$data = array('name' => 'Rob', 'age' => 40);
$newResponse = $oldResponse->withJson($data, 201);
or like this :
return $response->withStatus(xxx);
it doesnt work, this is my custom class
public function getCategories($response){
$dbconn = (new db())->connect();
//start db connection
try {
$stmt = $dbconn->prepare("SELECT category_id, category_name, category_description, category_type FROM article_category");
$categories = $stmt->fetchAll(PDO::FETCH_ASSOC);
$response->getBody()->write(json_encode($categories));
} catch (PDOException $e){
$response -> getBody() -> write(json_encode($this -> makeObject(704, 'Something went wrong!')));
}
}
What am i doing wrong here?