我正在使用slim框架使用php进行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"
我检查过方法的次数超过一次。我真的不知道问题可能是什么。在此先感谢您的帮助:)
答案 0 :(得分:2)
那是因为你有多行:
$app->run();
每个人都执行你的应用程序的整个逻辑,并提供整个输出。
在配置路由,中间件等后,应始终只执行一次Run方法。
看起来很奇怪,我在Slim docs中找不到这些信息(或任何关于Slim类的run方法的内容)......
答案 1 :(得分:0)
功能总是应该返回一些东西。我也遇到过这样的情况。但是我解决了它。在没有回声的任何函数中你应该尝试返回。确保它最好用。