自定义 class | 中的自定义响应代码苗条的框架 3
Custom response code in custom class | Slim Framework 3
嘿,我已经尝试瘦身好几天了,现在我可以像这样简单 API
$app->get('/articles/getcategories', function (Request $request, Response $response, array $args) {
(new Category) -> getCategories($response);
});
一个问题是当我想从我的 class 向我的端点发送自定义响应代码时,在这种情况下类别 class 它不起作用,我能够发送一个使用此方法的响应:
$response -> getBody() -> write(json_encode($this -> makeObject(704, 'Something went wrong!')));
但是当我查看文档时:
$newResponse = $response->withStatus(302);
$data = array('name' => 'Rob', 'age' => 40);
$newResponse = $oldResponse->withJson($data, 201);
或者像这样:
return $response->withStatus(xxx);
不行,这是我的习惯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!')));
}
}
我做错了什么?
您的 makeObject
-方法不会设置响应代码,它可能只会将其添加到响应对象。
您还需要设置 http 状态代码,然后 return 响应
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));
return $response; // uses default 200 response code (not needed but encouraged because without the return it can lead to problems later
} catch (PDOException $e){
$response -> getBody() -> write(json_encode($this -> makeObject(704, 'Something went wrong!')));
return $response->withStatus(704);
}
您还需要调整您的路线,将新的响应对象传递给 Slim
$app->get('/articles/getcategories', function (Request $request, Response $response, array $args) {
return (new Category)->getCategories($response);
});
注意:可以使用controller,那么只需要写一行,而不是上面的3行,
嘿,我已经尝试瘦身好几天了,现在我可以像这样简单 API
$app->get('/articles/getcategories', function (Request $request, Response $response, array $args) {
(new Category) -> getCategories($response);
});
一个问题是当我想从我的 class 向我的端点发送自定义响应代码时,在这种情况下类别 class 它不起作用,我能够发送一个使用此方法的响应:
$response -> getBody() -> write(json_encode($this -> makeObject(704, 'Something went wrong!')));
但是当我查看文档时:
$newResponse = $response->withStatus(302);
$data = array('name' => 'Rob', 'age' => 40);
$newResponse = $oldResponse->withJson($data, 201);
或者像这样:
return $response->withStatus(xxx);
不行,这是我的习惯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!')));
}
}
我做错了什么?
您的 makeObject
-方法不会设置响应代码,它可能只会将其添加到响应对象。
您还需要设置 http 状态代码,然后 return 响应
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));
return $response; // uses default 200 response code (not needed but encouraged because without the return it can lead to problems later
} catch (PDOException $e){
$response -> getBody() -> write(json_encode($this -> makeObject(704, 'Something went wrong!')));
return $response->withStatus(704);
}
您还需要调整您的路线,将新的响应对象传递给 Slim
$app->get('/articles/getcategories', function (Request $request, Response $response, array $args) {
return (new Category)->getCategories($response);
});
注意:可以使用controller,那么只需要写一行,而不是上面的3行,