Slim Framework Rest 服务获得两次输出

Slim Framework Rest service getting output twice

我正在 php 使用 slim 框架制作 REST 服务。一切正常,但有些奇怪。我总是得到两倍或三倍的数据。这是我的 index.php:

    <?php
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();

    if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
   header( "HTTP/1.1 200 OK" );
   exit();
}
function getConnection() {
    try {
        $db_username = "root";
        $db_password="admin";
        $conn = new PDO('mysql:host=localhost;dbname=dats24', $db_username);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
    return $conn;
}
$app->get('/problem/find/:id/','getProblem'); // Using Get HTTP Method and process getUser function
$app->get('/problem/find-all/','getProblems'); // Using Get HTTP Method and process getUsers function
$app->post('/problem/add/', 'addProblem'); // Using Post HTTP Method and process addUser function
$app->delete('/problem/delete/:id','deleteProblem'); // Using Delete HTTP Method and process deleteUser function
$app->run();

function getProblems() {
    $sql_query = "select * FROM problems ORDER BY Station";
    try {
        $dbCon = getConnection();
        $stmt   = $dbCon->query($sql_query);
        $problems  = $stmt->fetchAll(PDO::FETCH_OBJ);
        $dbCon = null;
        echo '{"probfems": ' . json_encode($problems) . '}';

    }
    catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }    
}

function getProblem($id) {
    $sql = "SELECT * FROM problems WHERE idproblems=:id";
    try {
        $dbCon = getConnection();
        $stmt = $dbCon->prepare($sql);  
        $stmt->bindParam("id", $id);
        $stmt->execute();
        $problem = $stmt->fetchObject();  
        $dbCon = null;
        echo json_encode($problem); 
    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}'; 
    }
}
function addProblem() {
    global $app;
    $postdata = file_get_contents("php://input");
    echo $postdata;
    $req = json_decode($postdata);; // Getting parameter with names
    $paramName = $req->station; // Getting parameter with names
    $paramAdres = $req->address; // Getting parameter with names
    $paramCity = $req->city;// Getting parameter with names
    $parampostal = $req->postalcode;
    $parampic = $req->pictureOfDamage;
    $paramdescrip= $req->description;
    $sql = "INSERT INTO problems (Station,Address,Postalcode,City,PictureOfDamage,Description) VALUES (:station,:address,:postalcode,:city,:pictureOfDamage,:description)";
    try {
        $dbCon = getConnection();
        $stmt = $dbCon->prepare($sql);  
        $stmt->bindParam(':station', $paramName);
        $stmt->bindParam(':address', $paramAdres);
        $stmt->bindParam(':city', $paramCity);
        $stmt->bindParam(':postalcode', $parampostal);
        $stmt->bindParam(':pictureOfDamage', $parampic);
        $stmt->bindParam(':description', $paramdescrip);
        $stmt->execute();
        $dbCon = null;
        echo json_encode("toegevoegd ");

    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}'; 
    }
}
function deleteProblem($id) {
    $sql = "DELETE FROM problems WHERE idproblems=:id";
    try {
        $dbCon = getConnection();
        $stmt = $dbCon->prepare($sql);  
        $stmt->bindParam("id", $id);
        $stmt->execute();
        $dbCon = null;
    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}'; 
    }
}
$app->run();

我选择哪种方法并不重要,例如 /find-all 将其作为输出(数据库当前为空):

{"problems": []}{"problems": []}{"problems": []}

如果我执行 POST,它会将它添加到数据库中两次。更奇怪的是,当我输入错误的 URL 时,我会收到双重 404 错误。

最后这是我的 .htaccess 文件

    RewriteEngine On
RewriteBase /Dats24/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
Header always set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Access-Control-Allow-Origin"

我检查过该方法的调用次数不超过一次。我真的不知道可能是什么问题。预先感谢您的帮助:)

那是因为你有多行:

  $app->run();

每个执行您的应用程序的整个逻辑,并提供整个输出。

运行 方法应该总是只执行一次,在你配置路由、中间件等之后

尽管看起来很奇怪,但我在 Slim docs...

中找不到此信息(或有关 Slim class 的 运行 方法的任何信息)

函数总应该returnsomething.I也遇到过这样的情况。但是我解决了 it.In 任何没有回声的函数你应该尝试 return.Sure 它会工作得最好。