沃梦达 / 编程问答 / php问题 / 正文

超薄框架REST服务两次获得输出

Slim Framework Rest service getting output twice(超薄框架REST服务两次获得输出)

本文介绍了超薄框架REST服务两次获得输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SLIM框架用php创建一个REST服务。一切都很正常,但有一些奇怪的地方。我总是得到两倍或三倍的数据。以下是我的index.php:

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

    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": []}

如果我预先形成一个帖子,它会将它两次添加到数据库中。更奇怪的是,当我输入错误的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();

每一个都执行应用程序的整个逻辑,并提供全部输出。

在配置路由、中间件等之后,Run方法应始终只执行一次。

奇怪的是,我在Slim docs...

中找不到此信息(或有关Slim类的Run方法的任何内容)...

这篇关于超薄框架REST服务两次获得输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:超薄框架REST服务两次获得输出

基础教程推荐