我剛剛通過作曲者安裝了slim,我試圖構建一個簡單的REST API。Slim 3框架 - setStatus上的致命錯誤
我當前的代碼如下:
require 'vendor/autoload.php';
$app = new \Slim\App();
$app->get('/getPoiInitialList', function ($request, $response, $args) {
//$app = \Slim\Slim::getInstance();
$app = new \Slim\App();
try
{
$db = getDB();
$sth = $db->prepare("SELECT * FROM wikivoyage_pois LIMIT 50");
$sth->execute();
$poiList = $sth->fetchAll(PDO::FETCH_OBJ);
if($poiList) {
$app->response->setStatus(200);
$app->response()->headers->set('Content-Type', 'application/json');
echo json_encode($poiList);
$db = null;
} else {
throw new PDOException('No records found.');
}
} catch(PDOException $e) {
$app->response()->setStatus(404);
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
});
// Run app
$app->run();
我有一些不修身發現錯誤,我是能夠通過,但現在我得到以下致命錯誤和通知時,我嘗試訪問端點上我的瀏覽器:
Notice: Undefined property: Slim\App::$response in C:\xampp\htdocs\api\index.php on line 47 - the first setStatus
和
Fatal error: Call to a member function setStatus() on null in C:\xampp\htdocs\api\index.php on line 47
在同一行。任何想法在這裏可能是錯誤的?
與您的代碼,我取消的通知,但我仍然得到致命錯誤:調用未定義的方法Slim \ Http \ Response :: setStatus()。 –
克勞迪奧,請更改代碼$ app-> response() - > setStatus(404);到$ app-> response-> setStatus(404); –
http://docs.slimframework.com/response/status/ –